Doubts about c# HTTPClient Proxy async method


Why is my code hint unable to connect to the x.x.x.x proxy server, but the proxy is normally available


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
   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

var httpClientHandler = new HttpClientHandler
                Proxy = new WebProxy("http://x.x.x.x:xxxx", false),
                UseProxy = true
            using (var client = new HttpClient(httpClientHandler))
                string apiUrl = "";
                HttpResponseMessage response = client.GetStringAsync(apiUrl).Result;
                string re =  response.Content.ReadAsStringAsync().Result;
asked on Stack Overflow Nov 13, 2019 by user11603749 • edited Nov 13, 2019 by user11603749

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0