【发布时间】:2015-04-05 10:31:51
【问题描述】:
我尝试使用 WebRequest 类连接到托管在不同服务器上的 Web 服务。 Web 服务返回一个字符串作为响应。这样做时我得到一个错误:
"System.Net.Sockets.SocketException: 无法建立连接 因为目标机器主动拒绝了”
System.Net.WebException: 无法连接到远程服务器 ---> System.Net.Sockets.SocketException:无法建立连接 因为目标机器在 127.0.0.1:14012 处主动拒绝了它 System.Net.Sockets.Socket.DoConnect(端点 endPointSnapshot, SocketAddress socketAddress) 在 System.Net.ServicePoint.ConnectSocketInternal(布尔连接失败, Socket s4, Socket s6, Socket&socket, IPAddress&地址, ConnectSocketState 状态、IAsyncResult asyncResult、Exception& 异常)---内部异常堆栈跟踪结束---在 System.Net.HttpWebRequest.GetRequestStream(TransportContext& 上下文) 在 System.Net.HttpWebRequest.GetRequestStream() 在 Limoallover.InsertSoapEnvelopeIntoWebRequestUS(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest) 在 Limoallover.dawUpdateDriverStatus(字符串 apiId,字符串 apiKey,字符串 idTrip,字符串tripCode,字符串状态码)在 Limoallover.UpdateJobStatus(String Lat, String Lng, Int32 DriverID, Int32 nJobStatus,Int32 nJobId,字符串 CompanyID,字符串 idTrip, 字符串tripCode,字符串状态码)
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 对我来说似乎不是不同的服务器?
-
127.0.0.1 重定向回您自己的计算机。防火墙问题?
-
127.0.0.1 是您的本地机器,也就是“localhost”。
-
确保您指向正确的网络服务地址
-
从命令窗口运行
netstat -ano以查看是否有任何东西正在侦听该地址/端口。