2015-02-05 68 views
14

我嘗試使用WebRequest類連接到託管在其他服務器上的Web服務。 Web服務返回一個字符串作爲Response。這樣做雖然我得到一個錯誤:由於目標機器主動拒絕,所以無法建立連接127.0.0.1

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

像TeamViewer,BitTorrent – TIKSN 2015-02-05 14:13:48

+3

關閉應用程序127.0.0.1似乎沒有不同的服務器給我? – 2015-02-05 14:14:32

+1

127.0.0.1重定向到您自己的計算機。防火牆問題? – Jeremy 2015-02-05 14:15:45

回答

21

六天後,我找到了讓我發瘋的答案!答案是禁用代理在web.config文件:

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

會很好,如果你包含一些示例代碼 – 2015-08-06 13:46:12

+1

如果我們應該禁用或啓用以便解決這個問題 – Zbun 2015-08-23 11:54:14

+0

我將導入的代碼示例現在 – 2015-08-29 13:04:51

0

有同樣的問題,事實證明這是Windows防火牆阻斷連接。嘗試禁用WindowsFirewall一段時間,看看是否有幫助,如果這是問題打開端口根據需要。

+0

謝謝你的回覆,但同樣的錯誤也出現我也想告訴你它一直在我的機器上工作,一旦發佈到服務器這個問題出現 – 2015-02-05 14:54:07

+0

好的,只要記住,積極拒絕意味着客戶端連接並出於某種原因被關閉,所以這個問題必須是服務器上的一些策略或類似的東西;不要生氣的客戶端配置它可能是好的 – sam 2015-02-05 14:59:16

+0

它讓我瘋狂你可以檢查它給我你可以通過團隊查看器連接到我的mac – 2015-02-05 15:00:29

6

當您聲明您的服務器正在不同的主機上運行時,異常消息顯示您嘗試連接到同一主機(127.0.0.1)。除了像URL中的「localhost」這樣的明顯錯誤,或者你可能想要檢查一下你的DNS設置。

+0

非常感謝我如何檢查我的DNS – 2015-02-05 14:54:44

1

存在阻止連接的防火牆,或者託管服務的進程沒有在該端口上偵聽。或者它正在不同的端口上收聽。

+0

當我關閉防火牆時出現同樣的問題 – 2015-02-05 14:55:06

1

我曾與以前運行良好的網站相同的問題。我通過從C:\WINDOWS\Microsoft.NET\Framework\v#.#.#####\Temporary ASP.NET Files\@proyectName\###CC##C\###C#CC刪除臨時文件解決了這個問題

1

我有一個類似的問題,試圖在我的VisualStudio 2013下調試運行基於WCF的HttpSelfHostServer。我嘗試了每個可能的方向(關閉防火牆,完全禁用IIS消除localhost端口被某些其他服務佔用的可能性等)。

最終,什麼「訣竅」(解決了問題)重新運行VisualStudio 2013爲管理員

令人驚歎。通過運行

0

刪除臨時文件>%TEMP%

而且通過以管理員身份運行打開VS2015,

它爲我工作。

0

如果您有配置文件轉換然後確保您在發佈配置文件中選擇了正確的配置。 (發佈>設置>配置)

1

當我開始小提琴手時,問題就消失了。 在問題出現之前在我的機器上使用它。 可能是與提琴手被代理的東西。

0

如果在Fiddler運行時有 - >在Fiddler中,請轉到'Rules'並禁用'Automatically Authenticate',它應該再次運行。

相關問題