我嘗試使用WebRequest類連接到託管在其他服務器上的Web服務。 Web服務返回一個字符串作爲Response。這樣做雖然我得到一個錯誤:由於目標機器主動拒絕,所以無法建立連接127.0.0.1
"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, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream() at Limoallover.InsertSoapEnvelopeIntoWebRequestUS(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest) at Limoallover.dawUpdateDriverStatus(String apiId, String apiKey, String idTrip, String tripCode, String statusCode) at Limoallover.UpdateJobStatus(String Lat, String Lng, Int32 DriverID, Int32 nJobStatus, Int32 nJobId, String CompanyID, String idTrip, String tripCode, String statusCode)
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);
}
}
像TeamViewer,BitTorrent – TIKSN 2015-02-05 14:13:48
關閉應用程序127.0.0.1似乎沒有不同的服務器給我? – 2015-02-05 14:14:32
127.0.0.1重定向到您自己的計算機。防火牆問題? – Jeremy 2015-02-05 14:15:45