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

当前回答

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

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

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

net start MongoDB

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

其他回答

通常,连接脚本不会提到要使用的端口。例如:

$mysqli = mysqli_connect('127.0.0.0.1', 'user', 'password', 'database');

所以,要连接到一个不使用端口3306的管理器,你必须在连接请求上指定端口号:

$mysqli = mysqli_connect('127.0.0.0.1', 'user', 'password', 'database', '3307');

要检查MySQL或MariaDB数据库管理器上的连接,使用脚本: 里面(64)\ www \ testmysql.php 在浏览器地址栏中输入“http://localhost/testmysql.php”,然后根据参数修改脚本。

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

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;
        }
    }

I was facing this issue today. Mine was Asp.Net Core API and it uses Postgresql as the database. We have configured this database as a Docker container. So the first step I did was to check whether I am able to access the database or not. To do that I searched for PgAdmin in the start as I have configured the same. Clicking on the resulted application will redirect you to the http://127.0.0.1:23722/browser/. There you can try access your database on the left menu. For me I was getting an error as in the below image.

输入密码,并尝试是否能够访问它。对我来说,这行不通。因为它是一个Docker容器,我决定重新启动我的Docker桌面,右键单击任务栏中的Docker图标,然后单击重新启动。

在重新启动Docker后,我能够登录并看到数据库,并且当我在Visual Studio中重新启动应用程序时,错误也消失了。

希望能有所帮助。

我也有。这是因为web服务的端口号意外地发生了变化。

当您有多个项目副本时,通常会发生此问题

我的项目正在使用我在Web中分配的特定端口号调用Web服务。配置文件我的主要项目文件。由于端口号意外更改,浏览器无法找到Web服务并抛出该错误。

我通过以下步骤解决了这个问题:(Visual Studio 2010)

进入Web服务项目的属性—>单击Web选项卡—>在服务器部分—>检查特定端口 然后分配主项目调用web服务时使用的标准端口号。

我希望这能解决问题。

欢呼:)

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