0

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);
}
1
  • 1
    Edited your question AGAIN to remove username and pwd. Please dont post sensitive data in public places. Commented Dec 4, 2009 at 18:06

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Are you sure the pwd/username is correct. If so try adding the domain as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.