CSharp - C# 无法连接,因为目标计算机主动拒绝它 127.0.0.1

我尝试使用WebRequest类连接到其他服务器上托管的web服务,web服务将字符串作为响应返回,在这样做时,我得到了一个错误:

"System.Net.Sockets.SocketException: no connection could be made because the target machine actively refused it "

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 :14012 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot , SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure , Socket s4 , Socket s6 , Socketsocket , IPAddressexception) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.GetRequestStream(TransportContext


private HttpWebRequest CreateWebRequestUS(string url, string action)


{


 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);


 webRequest.Headers.Add("SOAPAction", action);


 webRequest.ContentType = "text/xml;charset="utf-8"";


 webRequest.Accept = "text/xml";


 webRequest.Method = "POST";


 return webRequest;


}



private XmlDocument CreateSoapEnvelopeUS(string apiId, string apiKey, string idTrip, string tripCode, string statusCode)


{


 XmlDocument soapEnvelop = new XmlDocument();



 string xml = "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">";



 xml = xml + @"<soap:Body>";


 xml = xml + "<UpdateTripStatus xmlns="https://book.mylimobiz.com/api">";


 xml = xml + @"<apiId>" + apiId + "</apiId>";


 xml = xml + @"<apiKey>" + apiKey + "</apiKey>";


 xml = xml + @"<idTrip>" + idTrip + "</idTrip>";


 xml = xml + @"<tripCode>" + tripCode + "</tripCode>";


 xml = xml + @"<statusCode>" + statusCode + "</statusCode>";


 xml = xml + @"</UpdateTripStatus>";


 xml = xml + @"</soap:Body>";


 xml = xml + @"</soap:Envelope>";



 soapEnvelop.LoadXml(xml);


 return soapEnvelop;


}



private static void InsertSoapEnvelopeIntoWebRequestUS(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)


{


 using (Stream stream = webRequest.GetRequestStream())


 {


 soapEnvelopeXml.Save(stream);


 }


}



时间:

异常消息表示你正在尝试连接到同一主机(127.0.0.1 ),而且你正在声明服务器在不同的主机上运行,除了在url中有"localhost "的明显的Bug,或者可能需要检查你的DNS设置。

有一个防火墙阻止连接或托管该服务的进程没有侦听该端口。或者它正在监听另一个端口。

...