c# - 无法建立连接,因为目标机器主动拒绝 127.0.0.1

标签 c# asp.net .net sockets

我尝试使用 WebRequest 类连接到托管在不同服务器上的 Web 服务。 Web 服务返回一个字符串作为响应。这样做时出现错误:

"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);
    }
}

最佳答案

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

<system.net>
  <defaultProxy> 
    <proxy usesystemdefault="False"/> 
  </defaultProxy>
</system.net>

关于c# - 无法建立连接,因为目标机器主动拒绝 127.0.0.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28346177/

相关文章:

c# - 捕获鼠标在窗体上的任意位置单击(没有 IMessageFilter)

c# - 在 Controller mvc 5 中迭代 IEnumerable ViewData

asp.net - Controller 调用另一个 Controller C# WebApi

c# - 如何控制TemplateField顺序

c# - 局部变量实际上在 CLR 中分配在哪里?

c# - .NET 项目中的 ToolsVersion 规范

c# - .NET Framework - 有什么方法可以让 Dictionary<> 更快一点?

c# - 如果未呈现按钮,客户端能否以编程方式获取按钮单击事件?

asp.net - 只读 session 可写

c# - 我是否应该为添加到 System.Runtime.Caching.ObjectCache 的每个项目创建一个新的 CacheItemPolicy?