目标计算机积极拒绝,无法建立连接?

371
有时我在对 WebService 进行 HttpWebRequest 操作时会遇到以下错误,下面是我复制的代码:
System.Net.WebException: 无法连接到远程服务器---> System.Net.Sockets.SocketException: 由于目标计算机积极拒绝,无法建立连接 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();

3
我刚刚连接到Azure时遇到了这个问题。可以非常肯定地说,目标机器可用。重启和路由器重置后问题仍然存在,很奇怪。出问题的原因是Fiddler没有正确关闭。系统仍然期望通过Fiddler来路由流量,而这就是不可用的“目标机器”。 - Vok
这可能与使用的套接字有关吗?您在退出时关闭它们了吗? - George Sp
Xampp或Ampps => 启动/停止Mysql连接 - undefined
32个回答

0
我遇到了这个错误,花了一些时间来解决它。在我的情况下,我将https和net.tcp配置为IIS绑定在同一个端口上。显然,您不能在同一个端口上有两个东西。我使用了netstat -ap tcp命令来检查是否有东西在该端口上监听。没有任何监听。删除不必要的绑定(在我的情况下是https)解决了我的问题。

0

这是我自己的一个愚蠢问题,我在web.config中添加了一个默认代理以便在Fiddler中拦截流量,然后忘记将其删除!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接