Getting (401) Unautorised access when accessing a web service

Last post 04-03-2007 2:20 AM by XiaoYong Dai – MSFT. 3 replies.

Sort Posts:

  • Getting (401) Unautorised access when accessing a web service

    03-28-2007, 1:41 PM
    • Loading...
    • johanfr
    • Joined on 10-06-2006, 8:29 AM
    • Valencia, Spain
    • Posts 2

    Hi!

    After searching this (and others) forum I feel more stucked than before! What I try to do is sending XML to the service and then read the answer. But all I get is a 401.

    Are there anyone that could bring a bit of light over why it wont work? See my code below...

    slURL

    ="https://secure.server.com/jsp/merchant/xml/paymentService.jsp";
    slUsername = "USERNAME";
    slPassword = "password";
    string rawTransfer = "xml content that is verified to be correct";

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(slURL));
    webRequest.Method = "POST";
    webRequest.ContentLength = rawTransfer.Length;
    webRequest.ContentType = "text/xml";
    webRequest.AllowAutoRedirect = true;
    CredentialCache wrCache = new CredentialCache();
    wrCache.Add(new ("https://secure.server.com/jsp/merchant/xml/"), "Basic", new NetworkCredential(slUsername, slPassword, null));
    webRequest.Credentials = wrCache;
    webRequest.UseDefaultCredentials = false;
    webRequest.PreAuthenticate = true;

    try
    {
    byte[] xmlBytes = Encoding.ASCII.GetBytes(rawTransfer);
    Stream reqStream = webRequest.GetRequestStream();
    reqStream.Write(xmlBytes, 0, xmlBytes.Length);
    reqStream.Close();
    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
    StreamReader stream = new StreamReader(webResponse.GetResponseStream());
    string responseString = stream.ReadToEnd();
    stream.Close();
    ListBox1.Items.Add(responseString);
    return true;
    }
    catch (Exception ex)
    {
    ListBox1.Items.Add(ex.Message);
    }

    Cheers

    Johan

  • Re: Getting (401) Unautorised access when accessing a web service

    03-28-2007, 7:37 PM
    • Loading...
    • cathal
    • Joined on 06-18-2002, 4:02 PM
    • Belfast, Northern Ireland
    • Posts 2,697
    • TrustedFriends-MVPs

    do you have the exact 401 failure, there are 5 (401.1 through to 401.5). They're all authentication failures, but depending on the subcode could be user, server, filter or ACL based.

    Cathal

  • Re: Getting (401) Unautorised access when accessing a web service

    03-29-2007, 5:52 AM
    • Loading...
    • johanfr
    • Joined on 10-06-2006, 8:29 AM
    • Valencia, Spain
    • Posts 2

    No! I only have the error message caught by the exception handler and that is:

    "The remote server returned an error: (401) Unauthorized."

    The code below works most of the time, but cause a thread crash on the web server now and then.
    Error: "Exception: System.Threading.ThreadAbortException: Thread was being aborted.at MSXML2.XMLHTTPClass.get_responseText() at checkout3.MakeCardPayment()"

    XMLHTTP xmlHttp_ = new XMLHTTP();

                string ServerURL = "https://secure.server.com/jsp/merchant/xml/paymentService.jsp";
                xmlHttp_.open("POST",
                              @ServerURL,
                              false,
                              "USERNAME",
                              "password");
                xmlHttp_.setRequestHeader("Content-Type", "text/xml");
                xmlHttp_.send(sb.ToString());
                xresp = xmlHttp_.responseText;

    Cheers
    /Johan

  • Re: Getting (401) Unautorised access when accessing a web service

    04-03-2007, 2:20 AM

    Hi johanfr

    Does it work well if "enable anonymous access" is clicked in IIS? As a first step, make sure you can access the website without having to input username and password. 

    BTW, I wonder if you'd mind provide more details about the IIS log file output with errors and full error code?

    Please refer to the following status code in IIS log for more information.

    401 - Access denied. IIS defines a number of different 401 errors that indicate a more specific cause of the error.

    401.1 - Logon failed.
    401.2 - Logon failed due to server configuration.
    401.3 - Unauthorized due to ACL on resource.
    401.4 - Authorization failed by filter.
    401.5 - Authorization failed by ISAPI/CGI application.
    401.7 – Access denied by URL authorization policy on the Web server. This error code is specific to IIS 6.0.

    Hope it helps

    Best Regards
    XiaoYong Dai
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter