No connection could be made because the target machine actively refused it?

后端 未结 28 2210
北荒
北荒 2020-11-22 01:27

Sometimes I get the following error while I was doing HttpWebRequest to a WebService. I copied my code below too.


System.Net.WebException: Unable to connect         


        
相关标签:
28条回答
  • 2020-11-22 01:38

    I think, you need to check your proxy settings in "internet options". If you are using proxy/'hide ip' applications, this problem may be occurs.

    0 讨论(0)
  • 2020-11-22 01:38

    It was a silly issue on my side, I had added a defaultproxy to my web.config in order to intercept traffic in Fiddler, and then forgot to remove it!

    0 讨论(0)
  • 2020-11-22 01:39

    I had this issue happening often. I found SQL Server Agent service was not running. Once I started the service manually, it got fixed. Double check if the service is running or not:

    1. Run prompt, type services.msc and hit enter
    2. Find the service name - SQL Server Agent(Instance Name)

    If SQL Server Agent is not running, double-click the service to open properties window. Then click on Start button. Hope it will help someone.

    0 讨论(0)
  • 2020-11-22 01:41

    For service reference within a solution.

    1. Restart your workstation

    2. Rebuild your solution

    3. Update service reference in WCFclient project

    At this point, I received messsage (Windows 7) to allow system access. Then the service reference was updated properly without errors.

    0 讨论(0)
  • 2020-11-22 01:41

    I would like to share this answer I found because the cause of the problem was not the firewall or the process not listening correctly, it was the code sample provided from Microsoft that I used.

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

    I implemented this function almost exactly as written, but what happened is I got this error:

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

    This code would say the socket is connected, but not under the correct IP address actually needed for proper communication. (Provided by Microsoft)

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

    I re-wrote the code to just use the first valid IP it finds. I am only concerned with IPV4 using this, but it works with localhost, 127.0.0.1, and the actually IP address of you network card, where the example provided by Microsoft failed!

        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;
            }
        }
    
    0 讨论(0)
  • 2020-11-22 01:41

    I came across this error and took some time to resolve it. In my case I had https and net.tcp configured as IIS bindings on same port. Obviously you can't have two things on the same port. I used netstat -ap tcp command to check whether there is something listening on that port. There was none listening. Removing unnecessary binding (https in my case) solved my issue.

    0 讨论(0)
提交回复
热议问题