Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

c# - 无法连接,因为目标计算机主动拒绝了吗?(No connection could be made because the target machine actively refused it?)

Sometimes I get the following error while I was doing HttpWebRequest to a WebService.

(有时在对WebService执行HttpWebRequest时收到以下错误。)

I copied my code below too.

(我也复制了下面的代码。)


System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 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()

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();
  ask by hsnkvk translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If this happens always, it literally means that the machine exists but that it has no services listening on the specified port, or there is a firewall stopping you.

(如果总是发生这种情况,则从字面上意味着这台机器存在,但没有服务在指定端口上侦听,或者有防火墙阻止您。)

If it happens occasionally - you used the word "sometimes" - and retrying succeeds, it is likely because the server has a full 'backlog'.

(如果偶尔发生-您使用了“有时”一词-并且重试成功,则可能是因为服务器具有完整的“积压”。)

When you are waiting to be accept ed on a listening socket, you are placed in a backlog.

(当您等待在侦听套接字上被accept ,您将被积压。)

This backlog is finite and quite short - values of 1, 2 or 3 are not unusual - and so the OS might be unable to queue your request for the 'accept' to consume.

(此积压是有限的并且很短-1、2或3的值并不罕见-因此操作系统可能无法将您的请求接受排队以使用“接受”。)

The backlog is a parameter on the listen function - all languages and platforms have basically the same API in this regard, even the C# one .

(待办事项是listen功能上的一个参数-在这方面,所有语言和平台都具有基本相同的API,甚至C#也是如此 。)

This parameter is often configurable if you control the server, and is likely read from some settings file or the registry.

(如果您控制服务器,则该参数通常是可配置的,并且很可能从某些设置文件或注册表中读取。)

Investigate how to configure your server.

(研究如何配置服务器。)

If you wrote the server, you might have heavy processing in the accept of your socket, and this can be better moved to a separate worker-thread so your accept is always ready to receive connections.

(如果编写服务器,则套接字的接受过程中可能要进行大量处理,最好将其移至单独的工作线程中,以便接受总是准备好接收连接。)

There are various architecture choices you can explore that mitigate queuing up clients and processing them sequentially.

(您可以探索多种体系结构选择,以减轻排队和顺序处理客户端的麻烦。)

Regardless of whether you can increase the server backlog, you do need retry logic in your client code to cope with this issue - as even with a long backlog the server might be receiving lots of other requests on that port at that time.

(不管是否可以增加服务器积压,您都确实需要在客户端代码中使用重试逻辑来解决此问题-因为即使积压很长时间,服务器当时可能会在该端口上收到许多其他请求。)

There is a rare possibility where a NAT router would give this error should its ports for mappings be exhausted.

(如果NAT路由器的映射端口用尽,则极有可能出现此错误。)

I think we can discard this possibility as too much of a long shot though, since the router has 64K simultaneous connections to the same destination address/port before exhaustion.

(我认为我们可以放弃这种可能性,因为路由器会在耗尽之前将64K同时连接到相同的目标地址/端口。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...