我可以通过实现类 IWebProxy 并执行以下操作来使用代理: HttpClientHandler aHandler = new HttpClientHandler();
aHandler.UseCookies = true;
aHandler.AllowAutoRedirect = true;
IWebProxy proxy = new AWProxy(new Uri("http://xx.xx.xxx.xxx:xxxx"));
proxy.Credentials = new NetworkCredential("xxxx", "xxxx");
aHandler.Proxy = proxy;
HttpClient client = new HttpClient(aHandler);
通过调用 client.GetAsync(" http://google.com ") 我能够获得成功的响应消息。但是我希望能够使用 HttpRequestMessage 来控制标题和发布/放置内容。
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);
//add other headers
requestMessage.Headers.UserAgent.ParseAdd("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)");
HttpResponseMessage response = await client.SendAsync(requestMessage);
但是如果我使用 HttpRequestMessage 我会得到这个异常:
System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: Unable to connect to the remote server. ---> System.NullReferenceException: Object reference not set to an instance of an object.
任何帮助表示赞赏,谢谢!