无法建立连接,因为目标机器主动拒绝 127.0.0.1
No connection could be made because the target machine actively refused it 127.0.0.1
我尝试使用 WebRequest Class 连接到托管在不同服务器上的 Web 服务。 Web 服务 returns 一个字符串作为响应。这样做时出现错误:
"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);
}
}
有同样的问题,原来是Windows防火墙阻止了连接。尝试禁用 Windows 防火墙一段时间,看看是否有帮助,如果是问题,请根据需要打开端口。
异常消息表示您正在尝试连接到同一主机 (127.0.0.1),而您声明您的服务器 运行 在另一台主机上。除了明显的错误,例如 url 中有 "localhost",或者您可能想检查您的 DNS 设置。
防火墙阻止了连接,或者托管服务的进程未侦听该端口。或者它正在侦听不同的端口。
六天后,我找到了让我疯狂的答案!答案是在 web.config 文件处禁用代理:
<system.net>
<defaultProxy>
<proxy usesystemdefault="False"/>
</defaultProxy>
</system.net>
我在一个以前 运行 没问题的网站上遇到了同样的问题。我通过从 C:\WINDOWS\Microsoft.NET\Framework\v#.#.#####\Temporary ASP.NET Files\@projectName\###CC##C\###C#CC
中删除临时文件解决了这个问题
我有一个类似的问题,试图在我的 VisualStudio 2013 下调试 运行 基于 WCF 的 HttpSelfHostServer。我尝试了所有可能的方向(关闭防火墙,完全禁用 IIS 以消除本地主机端口可能被其他服务占用等)。
最终,"did the trick"(解决了问题)重新运行将 VisualStudio 2013 作为 管理员。
太棒了。
通过 运行 > %temp%
删除临时文件
然后 运行 以管理员身份打开 VS2015,
对我有用。
如果您有配置文件转换,请确保您在发布配置文件中选择了正确的配置。 (发布 > 设置 > 配置)
当我启动 Fiddler 时,这个问题对我来说消失了。
在出现问题之前在我的机器上使用它。
可能是 Fiddler 被代理了。
如果在 Fiddler 处于 运行 -> Fiddler 时,请转到 'Rules' 并禁用 'Automatically Authenticate',它应该会再次工作。
为代理设置添加 new WebProxy(),您将在其中创建 Web 请求。
示例:-
string url = "Your URL";
System.Net.WebRequest req = System.Net.WebRequest.Create(url);
req.Proxy = new WebProxy();
System.Net.WebResponse resp = req.GetResponse();
其中 req.Proxy = new WebProxy() 处理代理设置并帮助代码正常工作。
我尝试使用 WebRequest Class 连接到托管在不同服务器上的 Web 服务。 Web 服务 returns 一个字符串作为响应。这样做时出现错误:
"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);
}
}
有同样的问题,原来是Windows防火墙阻止了连接。尝试禁用 Windows 防火墙一段时间,看看是否有帮助,如果是问题,请根据需要打开端口。
异常消息表示您正在尝试连接到同一主机 (127.0.0.1),而您声明您的服务器 运行 在另一台主机上。除了明显的错误,例如 url 中有 "localhost",或者您可能想检查您的 DNS 设置。
防火墙阻止了连接,或者托管服务的进程未侦听该端口。或者它正在侦听不同的端口。
六天后,我找到了让我疯狂的答案!答案是在 web.config 文件处禁用代理:
<system.net>
<defaultProxy>
<proxy usesystemdefault="False"/>
</defaultProxy>
</system.net>
我在一个以前 运行 没问题的网站上遇到了同样的问题。我通过从 C:\WINDOWS\Microsoft.NET\Framework\v#.#.#####\Temporary ASP.NET Files\@projectName\###CC##C\###C#CC
我有一个类似的问题,试图在我的 VisualStudio 2013 下调试 运行 基于 WCF 的 HttpSelfHostServer。我尝试了所有可能的方向(关闭防火墙,完全禁用 IIS 以消除本地主机端口可能被其他服务占用等)。
最终,"did the trick"(解决了问题)重新运行将 VisualStudio 2013 作为 管理员。
太棒了。
通过 运行 > %temp%
删除临时文件然后 运行 以管理员身份打开 VS2015,
对我有用。
如果您有配置文件转换,请确保您在发布配置文件中选择了正确的配置。 (发布 > 设置 > 配置)
当我启动 Fiddler 时,这个问题对我来说消失了。 在出现问题之前在我的机器上使用它。 可能是 Fiddler 被代理了。
如果在 Fiddler 处于 运行 -> Fiddler 时,请转到 'Rules' 并禁用 'Automatically Authenticate',它应该会再次工作。
为代理设置添加 new WebProxy(),您将在其中创建 Web 请求。
示例:-
string url = "Your URL";
System.Net.WebRequest req = System.Net.WebRequest.Create(url);
req.Proxy = new WebProxy();
System.Net.WebResponse resp = req.GetResponse();
其中 req.Proxy = new WebProxy() 处理代理设置并帮助代码正常工作。