2012-03-19 210 views
2

我有一個很奇怪的問題。嘗試GetRequestStream()時無法連接到遠程服務器()

我有一個通過WCF稱爲通過Windows服務託管方法,而該方法嘗試做GetRequestStream(),我們得到一個異常「無法連接到遠程服務器

string strXMLServer = "https://xxxx.webex.com/WBXService/XMLService"; 

     WebRequest request = WebRequest.Create(strXMLServer); 
     // Set the Method property of the request to POST. 
     request.Method = "POST"; 
     // Set the ContentType property of the WebRequest. 
     request.ContentType = "application/x-www-form-urlencoded"; 

     string strXML = XMLmanager.createXML(User, Password, requestType, sessionKey); 

     byte[] byteArray = Encoding.UTF8.GetBytes(strXML); 

     // Set the ContentLength property of the WebRequest. 
     request.ContentLength = byteArray.Length; 

     // Get the request stream. 
     Stream dataStream = request.GetRequestStream(); //<--- here the exception!! 
     // Write the data to the request stream. 
     dataStream.Write(byteArray, 0, byteArray.Length); 
     // Close the Stream object. 
     dataStream.Close(); 
     // Get the response. 
     WebResponse response = request.GetResponse(); 

奇怪的是,當我嘗試運行這個應用程序作爲stanalone(控制檯應用程序),我沒有問題,並沒有錯誤!只有在我通過WCF調用方法時纔會出現異常!

回答

2

聽起來像你有這種SO question類似的情況。檢查您的Windows服務正在運行的帳戶,它很可能沒有訪問網絡資源。

+0

在本地系統下運行的Windows服務 – MoShe 2012-03-19 18:16:59

+0

在這種情況下,您需要使用作爲具有適當訪問權限的域帳戶進行HTTP調用或作爲機器帳戶(通常是計算機名稱)所在的網絡服務該域並具有適當的訪問權限。 – 2012-03-19 18:43:02

相關問題