我在尝试使用 Digest 身份验证与 Web 服务器 (Apache 2.2.17) 通信并使用 POST 方法发送数据时遇到问题:它总是返回 401 错误。但是,当我们不发布数据或 Fiddler 正在运行时(即使在发布数据时),它也能正常工作......您知道什么会导致问题吗?
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();
}