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:
- HttpClient and forms authentication in C#
- Login to the page with HttpWebRequest
- HTTP POST Returns Error: 417 "Expectation Failed."
- HTTP Post XML document - server receives only first line
- POST to HTTPS authentication error
- C# Xml in Http Post Request Message Body
I appreciate the help.
 
     
    