有时我得到以下错误,当我做HttpWebRequest到一个WebService。我也复制了下面的代码。


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();

当前回答

当你调用服务只有HTTP(例如:http://example.com),你调用HTTPS(例如:https://example.com),你得到的恰恰是这个错误-“没有连接可以建立,因为目标机器主动拒绝它”

其他回答

我从我的web层引用位于WCFHost上的服务时收到了这个错误。对我有效的方法可能并不适用于所有人,但我把这个答案留给那些可能适用的人。我的WCFHost的端口号是由IIS随机更新的,我只需要在我的web配置中更新到svc引用的结束路由。问题解决了。

我遇到了同样的错误,因为当你的服务器和客户端运行在同一台机器上,客户端需要服务器本地ip地址而不是公共ip地址来与服务器通信,你只需要公共ip地址,当服务器和客户端运行在不同的机器上,所以在客户端程序中使用本地ip地址来连接服务器,使用这种方法可以找到本地ip地址。

 public static string Getlocalip()
    {
        try
        {
            IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
            return localIPs[7].ToString();
        }
        catch (Exception)
        {

            return "null";
        }

    }

最可能的原因是防火墙。

这篇文章包含了一组可能对您有用的原因。

从文章来看,可能的原因有:

FTP服务器设置 软件/个人防火墙设置 多种软件/个人防火墙 杀毒软件 LSP层 路由器固件 关闭电脑 电脑未插电 提琴手

我有同样的错误与我的WCF服务使用Net TCP绑定,但解决后启动以下服务在我的情况。

Net.Pipe.Listener.Adapter

Net.TCP.Listener.Adapter

网Tcp端口共享服务

这里还有一件事要检查,我犯这个错误的次数比我愿意承认的多:你是试图连接https还是http?我以为我有一些模糊的证书问题,但结果是我忘记了我的URL定义中的s。总是先检查基础!