2

クラス IWebProxy を実装して次のようにすることで、プロキシを使用できます。

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.

どんな助けでも大歓迎です、ありがとう!

4

1 に答える 1

0

それらを HttpClient に直接追加します。

client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0;     Windows NT 6.2; WOW64; Trident/6.0)");
于 2013-06-27T21:19:41.603 に答える