1

I'm trying to send some XML through HTTP Post. But I'm getting 401 Unauthorized. Funny thing is when I access the URL through the browser, the username/password I provide are OK.

Here is my method signature:

        internal static string Send(string URL, string XMLdata, string RequestMethod, 
                                    string RequestUsername, string RequestPassword)

I have tried this as method body:

        string requestData = XMLdata;
        string responseData = "";

        StreamWriter myWriter = null;
        HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(URL);

        objRequest.Method = RequestMethod;
        objRequest.PreAuthenticate = true;
        objRequest.ContentType = "application/x-www-form-urlencoded";//"text/xml";
        objRequest.Credentials = new NetworkCredential(RequestUsername, RequestPassword);;
        objRequest.ContentLength = requestData.Length;

        try
        {
            myWriter = new StreamWriter(objRequest.GetRequestStream());
            myWriter.Write(requestData);
        }
        finally
        {
            myWriter.Close();
        }

        StreamReader sr = null;

        try
        {
            HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
            sr = new StreamReader(objResponse.GetResponseStream());
            responseData = sr.ReadToEnd();
        }
        finally
        {
            if (sr != null)
                sr.Close();
        }

        return responseData;

And also this:

        string result;

        using (var client = new WebClient())
        {
            Uri requestUri = new Uri(URL);
            CredentialCache cache = new CredentialCache();
            NetworkCredential nc = new NetworkCredential(RequestUsername, RequestPassword);
            cache.Add(requestUri, "Basic", nc);
            client.Credentials = cache;

            result = client.UploadString(requestUri, XMLdata);
        }

        return result;

Both get the 401. What am I doing wrong? Just a reminder: when I access the URL through the browser, it prompts for username/password and the credentials I provide are OK.

My readings on the subject so far:

I appreciate the help.

11
  • Maybe the authentication type is NTLM? Commented May 24, 2011 at 19:49
  • I don't have an answer for you, from your description, but I do have a suggestion: use Fiddler to see what the request and response look like, both when the browser request succeeds, and when the programmatic request fails. I suspect there will be a difference that jumps out at you. Commented May 24, 2011 at 19:51
  • "it prompts for username/password" - in a popup window? Then it is Win auth. Commented May 24, 2011 at 19:52
  • Yes, it prompts in a browser popup window, no matter what browser. Are you sure that makes this a Windows Authentication? Either way, do you know what I change in the codes I posted? Commented May 24, 2011 at 19:56
  • @jwismar Great tip, will do and let you know the outcome. BTW, you should post that as an answer Commented May 24, 2011 at 19:58

2 Answers 2

1

[At request of OP, I'm migrating my comment to an answer]

I don't have an answer for you, from your description, but I do have a suggestion: use Fiddler to see what the request and response look like, both when the browser request succeeds, and when the programmatic request fails. I suspect there will be a difference that jumps out at you.

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

1 Comment

I can tell you, as useful as this hint mat have been, what a pain debugging messages is! So far, have not discovered it yet...
1

I solved it. Actually I was misled by the provider to make a mistake with the URL. The reason I'm posting my answer is for future reference, if someone ever comes across this.

The provider emailed me with the URL http://api.providersurl.com. When I get that into browser, it gets redirected to https://api.providersurl.com (HTTPS!). Then, I'm able to authorize with the credentials.

The funny thing that kept me banging my head against the wall is that by accessing the HTTP URL, I did not get 404 (Not Found) nor 302 (Redirect). I got 401 (Unauthorized). So I just kept trying! And boy, did I try...

When I changed the URL in the code to HTTPS, authorization worked with BOTH codes I posted.

It took a few hours... But no time is waste of time when you are learning a getting experienced.

Comments

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.