No connection could be made because the target mac

2018-12-31 07:38发布

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

23条回答
琉璃瓶的回忆
2楼-- · 2018-12-31 08:10

This happened to me too.. Sometimes when I open my project this error shown up which was frustrating. The problem was that sometimes the port-number of web service was changing unexpectedly.

This problem usually happens when you have more than one copies of the project

My project was calling the Web service with a specific port number which I assigned in the Web.Config file of my main project file. As the port number changed unexpectedly, the browser was unable to find the Web service and throwing that error.

I solved this by following the below steps: (Visual Studio 2010)

Go to Properties of the Web service project --> click on Web tab --> In Servers section --> Check Specific port and then assign the standard port number by which your main project is calling the web service.

I hope this will solve the problem.

Cheers :)

查看更多
只若初见
3楼-- · 2018-12-31 08:10

In my case, some domains worked, while some did not. Adding a reference to my organization's proxy Url in my web.config fixed the issue.

<system.net>
    <defaultProxy useDefaultCredentials="true">
      <proxy proxyaddress="http://proxy.my-org.com/" usesystemdefault="True"/>
    </defaultProxy>
</system.net>
查看更多
倾城一夜雪
4楼-- · 2018-12-31 08:13

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!

查看更多
无与为乐者.
5楼-- · 2018-12-31 08:14

In my scenario, I have two applications:

  • App1
  • App2

Assumption: App1 should listen to App2's activities on Port 5000

Error: Starting App1 and trying to listen, to a nonexistent ghost town, produces the error

Solution: Start App2 first, then try to listen using App1

查看更多
唯独是你
6楼-- · 2018-12-31 08:15

I faced same error because when your Server and Client run on same machine the Client need server local ip address not Public ip address to communicate with server you need Public ip address only in case when Server and Client run on separate machine so use Local ip address in client program to connect with server Local ip address can be found using this method.

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

            return "null";
        }

    }
查看更多
心情的温度
7楼-- · 2018-12-31 08:16

Most possible reason is a Firewall.

This article contains a set of reasons. It may be useful to you.

From the article, possible reasons could be:

  • FTP server settings
  • Software/Personal Firewall Settings
  • Multiple Software/Personal Firewalls
  • Anti-virus Software
  • LSP Layer
  • Router Firmware
  • Computer Turned Off
  • Computer Not Plugged In
查看更多
登录 后发表回答