The target machine actively refuses the connection, resulting in the inability to establish a connection.
P粉268654873
P粉268654873 2023-08-20 12:40:57
0
1
444
<p>Sometimes when I perform HttpWebRequest to a Web service, the following error occurs. I've also copied my code below. </p> <hr /> <pre>System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: The connection could not be established because the target computer actively refused 127.0.0.1:80 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.GetRequestStream() </pre> <hr /> <pre class="brush:php;toolbar:false;">ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.PreAuthenticate = true; request.Credentials = networkCredential(sla); request.Method = WebRequestMethods.Http.Post; request.ContentType = "application/x-www-form-urlencoded"; request.Timeout = v_Timeout * 1000; if (url.IndexOf("asmx") > 0 && parStartIndex > 0) { AppHelper.Logger.Append("#############" sla.ServiceName); using (StreamWriter reqWriter = new StreamWriter(request.GetRequestStream())) { while(true) { int index01 = parList.Length; int index02 = parList.IndexOf("="); if (parList.IndexOf("&") > 0) index01 = parList.IndexOf("&"); string parName = parList.Substring(0, index02); string parValue = parList.Substring(index02 1, index01 - index02 - 1); reqWriter.Write("{0}={1}", HttpUtility.UrlEncode(parName), HttpUtility.UrlEncode(parValue)); if (index01 == parList.Length) break; reqWriter.Write("&"); parList = parList.Substring(index01 1); } } } else { request.ContentLength = 0; } response = (HttpWebResponse)request.GetResponse();</pre> <p><br /></p>
P粉268654873
P粉268654873

reply all(1)
P粉852114752

If this happens all the time, then this actually means that the machine exists, but there is no service listening on the specified port, or there is a firewall blocking you.

If this happens occasionally - you used the word "sometimes" - and the retry succeeds, it's most likely because the server's 'backlog' is full.

While you are waiting to be accepted on a listening socket, you will be placed in a backlog. This backlog is finite and very short - values ​​of 1, 2 or 3 are not uncommon - so the operating system may not be able to queue your request to 'accept' for consumption.

The backlog is a parameter of the listen function - all languages ​​and platforms basically have the same API in this regard, even C#. If you control the server, this parameter is usually configurable and may be read from some configuration file or registry. Learn how to configure your server.

If you wrote the server, you probably have heavy processing in the accept of the socket, which could be better moved to a separate worker thread, so that your accept is always ready to receive connections. You can explore various architectural options to alleviate queuing and sequential processing on the client side.

Regardless, regardless of whether you can increase the server's backlog, your client code will need retrylogic to deal with this - because even if There is a long backlog, and the server may still receive many requests for other ports at that time.

There is a rare possibility that this error will occur if the NAT router runs out of mapped ports. I think this is too unlikely though, as the router can establish 64K simultaneous connections to the same destination address/port before running out of capacity.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!