我能夠通過實施類IWebProxy並執行此操作來使用代理: HttpClientHandler aHandler = new HttpClientHandler();使用WebProxy與HttpClient.SendAsync()和HttpRequestMessage
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.
任何幫助表示讚賞,謝謝!
什麼是NullReferenceException的調用堆棧?你確定它不在調用你的代理實現嗎? – TheESJ