无连接可以作出,因为目标机器积极地拒绝它? [英] No connection could be made because the target machine actively refused it?
问题描述
有时候我碰到下面的错误,而我在做的HttpWebRequest到WebService。我复制下面我太code。
System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:无连接可以作出,因为目标机器积极地拒绝它127.0.0.1:80 在System.Net.Sockets.Socket.DoConnect(端点endPointSnapshot,SocketAddress的SocketAddress的) 在System.Net.Sockets.Socket.InternalConnect(端点remoteEP) 在System.Net.ServicePoint.ConnectSocketInternal(布尔connectFailure,插座S4,S6插座,插座和插座,ip地址和地址,ConnectSocketState状态,IAsyncResult的asyncResult,的Int32超时,异常和异常) ---内部异常堆栈跟踪的结尾--- 在System.Net.HttpWebRequest.GetRequestStream()
ServicePointManager.CertificatePolicy =新TrustAllCertificatePolicy();
HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(URL);
请求preAuthenticate = TRUE。
request.Credentials =的NetworkCredential(SLA);
request.Method = WebRequestMethods.Http.Post;
request.ContentType =应用/的X WWW的形式urlen codeD;
request.Timeout = v_Timeout * 1000;
如果(url.IndexOf(ASMX)大于0&安培;&安培; parStartIndex大于0)
{
AppHelper.Logger.Append(#############+ sla.ServiceName);
使用(StreamWriter的reqWriter =新的StreamWriter(request.GetRequestStream()))
{
而(真)
{
INT INDEX01 = parList.Length;
INT index02 = parList.IndexOf(=);
如果(parList.IndexOf(与&)大于0)
INDEX01 = parList.IndexOf(与&);
串parName = parList.Substring(0,index02);
字符串parValue = parList.Substring(index02 + 1,INDEX01 - index02 - 1);
reqWriter.Write({0} = {1},HttpUtility.UrlEn code(parName),HttpUtility.UrlEn code(parValue));
如果(INDEX01 == parList.Length)
打破;
reqWriter.Write(与&);
parList = parList.Substring(INDEX01 + 1);
}
}
}
其他
{
request.ContentLength = 0;
}
响应=(HttpWebResponse)request.GetResponse();
如果出现这种情况总是,它的字面意思是,该机的存在,但它没有服务监听指定的端口上,或者是有防火墙阻止你。
如果是偶尔发生 - 你用了有时 - 并重试成功,这可能是因为服务器有一个完整的积压
当你正在等待接受一个监听套接字
版,你被放置在一个积压。这种积压是有限的,很短 - 1,2或3的值是不寻常 - 等操作系统可能无法排队的请求接受消耗
积压是一个参数听
功能 - 所有的语言和平台具有基本相同的API在这方面,连的C#~~V 之一。这个参数往往是可配置的,如果你控制了服务器,并有可能从某些设置文件或注册表中读取。调查如何配置你的服务器。
如果你写的服务器,你可能有大到处理在接受您的插座,这样就可以更好地转移到一个单独的工作线程使你接受的是随时准备接收连接。有各种架构选择,你可以探索缓解排队的客户,并依次处理它们。
不管你是否会增加服务器积压的,你确实需要的的重试的在客户端code逻辑来应付这种问题 - 因为即使长时间的积压服务器可能在那个时候接受该端口上很多其他的请求
有一种难得的可能性,其中一个NAT路由器来为这个错误应该它的映射端口耗尽。我认为,我们可以放弃这种可能性太长时间拍摄的,虽然,因为路由器具有64K到同一目的地址/端口同时连接耗尽之前。
Sometimes I get the following error while I was doing HttpWebRequest to a WebService. 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();
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. 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.
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. 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 it's ports for mappings be exhausted. 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.
这篇关于无连接可以作出,因为目标机器积极地拒绝它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!