2009-12-04 49 views
0

HTTP狀態401請求失敗:未經授權。爲什麼?在C#中開發的.net webservices中出現'未授權'錯誤。

TravelCodesTranslator ws = GetEncodeDecodeWS(); 

// Create and set up the credentials for XmlSelectWebService 
string UserName = "username"; 
string Password = "pwd"; 
// Xml Select uses Basic Authentication 
NetworkCredential credentials = new NetworkCredential(UserName, Password); 
CredentialCache cc = new CredentialCache(); 
ws.PreAuthenticate = true; 
ws.Credentials = credentials; 

XmlElement response; 

if (_requestType == "Encode") 
{ 
    response = ws.Encode(request.DocumentElement); // got error of 
                // "The request failed 
                // with HTTP status 401: 
                // Unauthorized." 
} 
else 
{ 
    response = ws.Decode(request.DocumentElement); 
} 
+1

再次編輯您的問題以刪除用戶名和密碼。請不要在公共場所張貼敏感數據。 – Shoban

回答

2

變化

ws.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; 

​​

提示:該錯誤信息是很清楚。它說你的憑據不正確。然後我會檢查用戶名和密碼是否正確。如果它們是正確的,那麼我會看看它們是否正確地分配給請求。

+0

仍然錯誤.... auauthiencation 401. – sikender

+0

你確定密碼/用戶名是正確的。如果是這樣,請嘗試添加域。 – Shoban