如何发送授权请求

发布于 2024-10-02 16:35:55 字数 837 浏览 3 评论 0原文

我以编程方式向远程服务器发送请求:

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 询问我时,我知道要授权的用户/通行证。

谁能帮我如何发送授权请求? 谢谢!

I send programatically a request to remote server:

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();

but this code throws error:

The remote server returned an error: (401) Unauthorized.

I know user/pass to autorize when IE ask me.

Could anyone help me how to send authorized request?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雪花飘飘的天空 2024-10-09 16:35:55
webClient.Credentials = new NetworkCredential("Login", "Password");
webClient.Credentials = new NetworkCredential("Login", "Password");
飞烟轻若梦 2024-10-09 16:35:55

服务器似乎使用 Windows 身份验证 - 尽管我只是猜测。如果是这样,请添加此行:

   request.Credentials = CredentialCache.DefaultCredentials;

Server seems to use Windows Authentication - although I am only guessing. If it is so, add this line:

   request.Credentials = CredentialCache.DefaultCredentials;
茶花眉 2024-10-09 16:35:55

看起来服务器正在使用 Windows 集成安全性?

尝试这样的事情

WebRequest req = WebRequest.Create(tokenUri);
req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
req.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

looks like the server is using windows integrated security?

try something like this

WebRequest req = WebRequest.Create(tokenUri);
req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
req.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文