'Unauthorized' error in .net webservices developed in C#
The request failed with HTTP status 401: Unauthorized. Why?
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);
}
Change
ws.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
to
ws.Credentials = credentials
Tip: The error message is quite clear. It says your credentials are not correct. Then I would check whether the username and password are correct. If they are correct then I would see if they are assigned properly to the request.
精彩评论