Why is my code hint unable to connect to the x.x.x.x proxy server, but the proxy is normally available
winform
private async Task<string> get_url(string url)
{
var httpClientHandler = new HttpClientHandler
{
Proxy = new WebProxy("http://xx.x.x.x:xxxx", false),
UseProxy = true
};
var request = await new HttpClient(httpClientHandler).GetAsync(url);
var re = await request.Content.ReadAsStringAsync();
return re;
}
Agent ip has been replaced by xxxx
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Net.Sockets.SocketException (0x80004005): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 1.2.3.4:xxxx
at System.Net.Sockets.Socket.InternalEndConnect(IAsyncResult asyncResult)
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
The following code is error free
Consoleļ¼
var httpClientHandler = new HttpClientHandler
{
Proxy = new WebProxy("http://x.x.x.x:xxxx", false),
UseProxy = true
};
using (var client = new HttpClient(httpClientHandler))
{
string apiUrl = "http://www.google.com";
HttpResponseMessage response = client.GetStringAsync(apiUrl).Result;
string re = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(re);
}
User contributions licensed under CC BY-SA 3.0