有时我得到以下错误,当我做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();

当前回答

好吧,我今天在64位的Windows 8上突然收到了这个错误,这是第一次,原来我的my.ini被重置了,bin/mysqld文件被删除了,以及“Program Files/MySQL/MySQL Server 5.6”文件夹中的其他项目。

为了解决这个问题,我不得不再次运行MySQL安装程序,只安装服务器,并从“ProgramData/MySQL/MySQL server 5.6”中复制my.ini文件的最新版本,在我的情况下命名为my_2014-03-28T15-51-20.ini(不知道为什么最近会复制到那里)回到“Program Files/MySQL/MySQL server 5.6”中。

自从MySQL工作以来,系统唯一的变化是安装了Native Instruments的Traktor 2和Traktor Audio 2声卡,这真的不应该引起这个问题,除了我之外没有其他人使用过这个系统。如果有人有线索,请评论以防止我和其他遇到这种情况的人。

其他回答

我也有同样的问题。问题是我没有启动硒服务器。我已经下载了硒服务器,并启动了它。启动硒服务器后,问题消失了,一切正常工作。

参考这个:http://coding-issues.blogspot.in/2012/11/no-connection-could-be-made-because.html

我认为,你需要检查你的代理设置在“互联网选项”。如果您正在使用代理/'隐藏ip'应用程序,则可能会出现此问题。

在Windows 7 Home Premium 64bit上使用WampServer 64bit时,我遇到了这个确切的问题。经过一小时又一小时的试验,很明显,我们所需要的只是在my.ini中注释掉一行。然后它工作得很好。

注释掉1行 套接字= mysql

如果你把旧的/data/文件放在合适的位置,WampServer将接受所有这些文件,除了它要重写的/mysql/文件夹。因此,然后我简单地从我以前的开发环境中导入/mysql/ user数据的备份,并在phpMyAdmin SQL窗口中运行FLUSH PRIVILEGES。伟大的工作。一定是出了什么问题,因为事情不应该这么容易。

我想分享我发现的这个答案,因为问题的原因不是防火墙或进程没有正确侦听,而是我使用的微软提供的代码示例。

https://msdn.microsoft.com/en-us/library/system.net.sockets.socket%28v=vs.110%29.aspx

我几乎完全按照所写的那样实现了这个函数,但发生的事情是我得到了这个错误:

2016-01-05 12:00:48075 [10] ERROR -错误是:System.Net.Sockets.SocketException (0x80004005): No connection could be made because The target machine actively refused it [fe80::caa:745:a1da:e6f1%11]:4080

这段代码将表示套接字已连接,但不是在正确通信实际所需的正确IP地址下。(微软提供)

private static Socket ConnectSocket(string server, int port)
    {
        Socket s = null;
        IPHostEntry hostEntry = null;

        // Get host related information.
        hostEntry = Dns.GetHostEntry(server);

        // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
        // an exception that occurs when the host IP Address is not compatible with the address family
        // (typical in the IPv6 case).
        foreach(IPAddress address in hostEntry.AddressList)
        {
            IPEndPoint ipe = new IPEndPoint(address, port);
            Socket tempSocket = 
                new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            tempSocket.Connect(ipe);

            if(tempSocket.Connected)
            {
                s = tempSocket;
                break;
            }
            else
            {
                continue;
            }
        }
        return s;
    }

我重写了代码,只使用它找到的第一个有效IP。我只关心使用IPV4,但它与localhost, 127.0.0.1,和您的网卡的实际IP地址,其中微软提供的例子失败了!

    private Socket ConnectSocket(string server, int port)
    {
        Socket s = null;

        try
        {
            // Get host related information.
            IPAddress[] ips;
            ips = Dns.GetHostAddresses(server);

            Socket tempSocket = null;
            IPEndPoint ipe = null;

            ipe = new IPEndPoint((IPAddress)ips.GetValue(0), port);
            tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            Platform.Log(LogLevel.Info, "Attempting socket connection to " + ips.GetValue(0).ToString() + " on port " + port.ToString());
            tempSocket.Connect(ipe);

            if (tempSocket.Connected)
            {
                s = tempSocket;
                s.SendTimeout = Coordinate.HL7SendTimeout;
                s.ReceiveTimeout = Coordinate.HL7ReceiveTimeout;
            }
            else
            {
                return null;
            }

            return s;
        }
        catch (Exception e)
        {
            Platform.Log(LogLevel.Error, "Error creating socket connection to " + server + " on port " + port.ToString());
            Platform.Log(LogLevel.Error, "The error is: " + e.ToString());
            if (g_NoOutputForThreading == false)
                rtbResponse.AppendText("Error creating socket connection to " + server + " on port " + port.ToString());
            return null;
        }
    }

对我来说,我想在shell中启动mongo(与问题的确切上下文无关,但在shell中启动mongo之前有相同的错误消息)

进程“MongoDB Service”没有在服务中运行

以管理员身份启动cmd并输入,

net start MongoDB

为了查看MongoDB是否启动并运行,只需输入mongo,在cmd中,它会给出mongo版本详细信息和mongo连接URL