我以编程方式向远程服务器发送请求:
string xml =“SomeXML Data”;
string url = @"http://someserver.com";
WebRequest request = WebRequest.Create(url);
request.Method = "Post";
request.ContentType = "text/xml";
//The encoding might have to be chaged based on requirement
UTF8Encoding encoder = new UTF8Encoding();
byte[] data = encoder.GetBytes(xml); //postbody is plain string of xml
request.ContentLength = data.Length;
Stream reqStream = request.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
System.Net.WebResponse response = request.GetResponse();
System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());
string str = reader.ReadToEnd();
但是这段代码会抛出错误:
远程服务器返回错误:(401)未经授权。
当IE问我时,我知道用户/传递自动化。
有人可以帮我发送授权请求吗? 谢谢!
答案 0 :(得分:1)
webClient.Credentials = new NetworkCredential("Login", "Password");
答案 1 :(得分:0)
服务器似乎使用Windows身份验证 - 虽然我只是在猜测。如果是这样,请添加以下行:
request.Credentials = CredentialCache.DefaultCredentials;
答案 2 :(得分:0)
看起来服务器正在使用Windows集成安全性?
尝试这样的事情
WebRequest req = WebRequest.Create(tokenUri);
req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
req.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;