Ahm*_*aid 19 .net c# sockets asp.net
我尝试使用WebRequest类连接到托管在不同服务器上的Web服务.Web服务返回一个字符串作为响应.这样做我得到一个错误:
"System.Net.Sockets.SocketException:无法建立连接,因为目标计算机主动拒绝它"
System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:无法建立连接,因为目标计算机在System.Net.Sockets.Socket上主动拒绝它127.0.0.1:14012 System.Net.ServicePoint.ConnectSocketInternal上的.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)(布尔值connectFailure,套接字s4,套接字s6,套接字和套接字,IP地址和地址,ConnectSocketState状态,IAsyncResult asyncResult,异常和异常)---内部异常堆栈结束跟踪---在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 Company ID,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);
}
}
Run Code Online (Sandbox Code Playgroud)
Ahm*_*aid 29
六天后,我找到了让我疯狂的答案!答案是在web.config文件中禁用代理:
<system.net>
<defaultProxy>
<proxy usesystemdefault="False"/>
</defaultProxy>
</system.net>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
118092 次 |
最近记录: |