我尝试使用WebRequest类连接到托管在不同服务器上的Web服务 . Web服务返回一个字符串作为响应 . 这样做我得到一个错误:
“System.Net.Sockets.SocketException:无法 Build 连接,因为目标计算机主动拒绝它”System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:没有连接因为目标机器在System.Net.Service.Service.ConnectSocketInternal的System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)中主动拒绝了它127.0.0.1:14012(布尔connectFailure,Socket s4,Socket s6,套接字和套接字,IP地址和地址,ConnectSocketState状态,IAsyncResult asyncResult,异常和异常)---内部异常堆栈跟踪的结束---在System.Net.HttpWebRequest.GetRequestStream()处的System.Net.HttpWebRequest.GetRequestStream(TransportContext&context)处在Limoallover.dawUpdateDriverStatus(String apiId,String apiKey,String idTrip,String tripCode,String statusCode)的Limoallover.InsertSoapEnvelopeIntoWebRequestUS(XmlDocument soapEnvelopeXml,HttpWebRequest webRequest)at 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);
}
}
11 回答
我遇到了类似的问题,尝试在我的VisualStudio 2013下运行基于WCF的HttpSelfHostServer调试 . 我尝试了所有可能的方向(关闭防火墙,完全禁用IIS以消除其他服务可能使用localhost端口等) .
最终,"did the trick"(解决了问题)将VisualStudio 2013重新运行为 Administrator .
惊人 .
当我开始提琴手时,这个问题就消失了 . 在遇到问题之前在我的机器上使用它 . 可能是Fiddler代理的东西 .
通过运行>%temp%删除临时文件
并以管理员身份运行打开VS2015,
这个对我有用 .
如果您有 config file transforms ,请确保在发布配置文件中选择了正确的配置 . (发布>设置>配置)
有同样的问题,原来是WindowsFirewall阻塞连接 . 尝试禁用WindowsFirewall一段时间,以查看是否有帮助,如果是问题打开端口根据需要 .
如果你在Fiddler运行时有这个 - >在Fiddler中,请转到'规则'并禁用'自动验证',它应该再次运行 .
六天后,我找到了让我疯狂的答案!答案是在web.config文件中禁用代理:
异常消息表明您正在尝试连接到同一主机(127.0.0.1),而您声明您的服务器正在另一台主机上运行 . 除了明显的错误,如在网址中有“localhost”,或者您可能想要检查您的DNS设置 .
有一个防火墙阻止连接或托管该服务的进程没有侦听该端口 . 或者它正在侦听不同的端口 .
我之前运行正常的网站遇到了同样的问题 . 我通过从
C:\WINDOWS\Microsoft.NET\Framework\v#.#.#####\Temporary ASP.NET Files\@proyectName\###CC##C\###C#CC
删除临时文件解决了这个问题为代理设置添加 new WebProxy() ,您将在其中创建Web请求 .
示例: -
req.Proxy = new WebProxy() 处理代理设置并帮助代码正常工作 .