无法建立连接,因为目标计算机主动拒绝它127.0.0.1

我尝试使用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,套接字和套接字,IPAddress和地址,ConnectSocketState状态,IAsyncResult asyncResult,exception和exception)—内部exception堆栈的结束trace —在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 = ""; xml = xml + @""; xml = xml + ""; xml = xml + @"" + apiId + ""; xml = xml + @"" + apiKey + ""; xml = xml + @"" + idTrip + ""; xml = xml + @"" + tripCode + ""; xml = xml + @"" + statusCode + ""; xml = xml + @""; xml = xml + @""; xml = xml + @""; soapEnvelop.LoadXml(xml); return soapEnvelop; } private static void InsertSoapEnvelopeIntoWebRequestUS(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest) { using (Stream stream = webRequest.GetRequestStream()) { soapEnvelopeXml.Save(stream); } } 

六天后,我找到了让我疯狂的答案! 答案是在web.config文件中禁用代理:

      

exception消息表明您正在尝试连接到同一主机(127.0.0.1),而您声明您的服务器正在另一台主机上运行。 除了明显的错误,如在url中有“localhost”,或者您可能想要检查您的DNS设置。

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

我之前运行正常的网站遇到了同样的问题。 我通过从C:\WINDOWS\Microsoft.NET\Framework\v#.#.#####\Temporary ASP.NET Files\@proyectName\###CC##C\###C#CC删除临时文件解决了这个问题C:\WINDOWS\Microsoft.NET\Framework\v#.#.#####\Temporary ASP.NET Files\@proyectName\###CC##C\###C#CC

我遇到了类似的问题,尝试在我的VisualStudio 2013下运行基于WCF的HttpSelfHostServer 。我尝试了所有可能的方向(关闭防火墙,完全禁用IIS以消除其他服务使用localhost端口的可能性等) 。

最终,“诀窍”(解决问题)是以管理员身份重新运行VisualStudio 2013。

惊人。

当我开始提琴手时,这个问题就消失了。 在遇到问题之前在我的机器上使用它。 可能是Fiddler代理的东西。

通过运行>%temp%删除临时文件

并以管理员身份运行打开VS2015,

这个对我有用。

如果您有配置文件转换,请确保在发布配置文件中选择了正确的配置。 (发布>设置>配置)

有同样的问题,原来是WindowsFirewall阻塞连接。 尝试禁用WindowsFirewall一段时间,以查看是否有帮助,如果是问题打开端口根据需要。

如果你在Fiddler运行时有这个 – >在Fiddler中,请转到’规则’并禁用’自动validation’,它应该再次运行。