hsn*_*kvk 228 .net c# socketexception system.net.webexception asp.net-web-api2
有时我在向WebService执行HttpWebRequest时遇到以下错误.我也复制了下面的代码.
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();
Run Code Online (Sandbox Code Playgroud)
Wil*_*ill 189
如果这种情况总是发生,它实际上意味着机器存在,但它没有服务侦听指定的端口,或者有防火墙阻止你.
如果偶尔发生 - 你使用"有时"这个词 - 并且重试成功,很可能是因为服务器有一个完整的"积压".
当您等待accept在侦听套接字上编辑时,您将被置于待办事项中.这个积压是有限的,非常短 - 值为1,2或3并不罕见 - 因此操作系统可能无法将您的'accept'请求排队.
积压是listen函数的一个参数- 所有语言和平台在这方面基本上都是相同的API,甚至是C#one.如果您控制服务器,此参数通常是可配置的,并且可能从某些设置文件或注册表中读取.研究如何配置服务器.
如果您编写了服务器,则可能在接受套接字时需要进行大量处理,这可以更好地移动到单独的工作线程中,这样您的接受就可以随时接收连接.您可以探索各种架构选择,以减少排队客户端并按顺序处理它们.
无论您是否可以增加服务器积压,您都需要在客户端代码中重试逻辑以应对此问题 - 即使长时间积压,服务器可能会在此时在该端口上接收大量其他请求.
如果NAT路由器的映射端口用尽,则NAT路由器很少会出现此错误.我认为我们可以放弃这种可能性,因为路由器在耗尽之前有64K同时连接到相同的目标地址/端口.
Mr_*_*een 15
这也发生在我身上.有时当我打开我的项目时,出现这个错误令人沮丧.问题是有时Web服务的端口号意外地发生了变化.
当您有多个项目副本时,通常会发生此问题
我的项目是使用我在主项目文件的Web.Config文件中指定的特定端口号调用Web服务.由于端口号意外更改,浏览器无法找到Web服务并抛出该错误.
我按照以下步骤解决了这个问题:(Visual Studio 2010)
转到
Web service项目的属性- >单击Web选项卡 - >在服务器部分 - >检查特定端口 ,然后分配standard port number主项目调用Web服务的位置.
我希望这能解决问题.
干杯:)
小智 5
我有同样的问题。问题是我没有启动硒服务器。我已经下载了 selenium 服务器并启动了它。启动硒服务器后,问题消失了,一切正常。
请参阅:http : //coding-issues.blogspot.in/2012/11/no-connection-could-be-made-because.html
我使用 Net TCP 绑定的 WCF 服务遇到了同样的错误,但在我的案例中启动以下服务后解决了。
网络.管道.监听器.适配器
网络.TCP.监听器.适配器
Net.Tcp端口共享服务
当您调用仅具有 HTTP 的服务(例如:http://example.com)并且调用 HTTPS(例如:https://example.com)时,您会收到以下错误 - “无法建立连接,因为目标机器主动拒绝”
| 归档时间: |
|
| 查看次数: |
1031789 次 |
| 最近记录: |