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

后端 未结 28 2761
北荒
北荒 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:51

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

提交回复
热议问题