I’m experiencing issues when trying to communicate with a web server (Apache 2.2.17) using Digest authentication, and sending data with the POST method: it always returns a 401 error. However, it works well when we don’t post data, or when Fiddler is running (even when posting data)…Do you have an idea of what can cause the problem?
public void DoRequest(string v_strURL, XmlDocument v_objXMLDoc)
{
var credentialCache = new CredentialCache();
credentialCache.Add(new Uri("http://" + ServerIP + "/"), "Digest", new NetworkCredential("admin", "test", "realm"));
String RequestContent = "request=" + v_objXMLDoc.InnerXml.Replace(' ', '+');
Uri Url = new Uri(v_strURL);
HttpWebRequest objHttpWebRequest;
HttpWebResponse objHttpWebResponse = null;
Stream objRequestStream = null;
byte[] bytes = new byte[0];
bytes = System.Text.Encoding.UTF8.GetBytes(RequestContent);
objHttpWebRequest = (HttpWebRequest)WebRequest.Create(v_strURL);
objHttpWebRequest.UserAgent = "MySampleCode";
objHttpWebRequest.Credentials = credentialCache;
objHttpWebRequest.Method = "POST";
objHttpWebRequest.ContentLength = bytes.Length;
objHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";
objHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
objRequestStream = objHttpWebRequest.GetRequestStream();
objRequestStream.Write(bytes, 0, bytes.Length);
objRequestStream.Close();
objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();
}
We finally have solved the problem: