我目前正在通过WebApi接收请求,并尝试将其转发到另一个站点。
目标是接收请求,例如:http://localhost:9999/#q=test。然后将其转发到真实的站点:(我设置了google.com进行测试)http://google.com/#q=test 以下是我的代码:
目标是接收请求,例如:http://localhost:9999/#q=test。然后将其转发到真实的站点:(我设置了google.com进行测试)http://google.com/#q=test 以下是我的代码:
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
string url = request.RequestUri.PathAndQuery;
UriBuilder forwardUri = new UriBuilder(_otherWebSiteBase);
forwardUri.Path = url;
if (request.Method == HttpMethod.Get)
{
//request.Method = HttpMethod.Post;
}
request.RequestUri = forwardUri.Uri;
request.Headers.Host = forwardUri.Host;
return await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);//_client is an HttpClient
}
我目前遇到了一个 System.Net.ProtocolViolationException
异常,它显示:
Cannot send a content-body with this verb-type.
但我的请求方式是GET(应该是GET),如果我使用POST请求,就没有异常发生,但Google表示他们不希望收到POST请求。
那么这个异常是为什么呢?有什么解决方法吗?