在 C# 中重用 HttpWebRequest 的连接
我需要使用 .Net 发出 POST 请求。
我可以通过 GET 进行身份验证,因此我尝试在同一连接上发出 POST 请求以保持身份验证。
问题是我收到 401 Not Authenticated 异常,这意味着连接尚未被重用。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("my-server");
request.Credentials = new NetworkCredential("user", "password");
request.GetResponse().Close(); // Works fine
// Now the request I want to make...
request = (HttpWebRequest)WebRequest.Create("my-server");
request.Credentials = new NetworkCredential("user", "password");
request.Method = "post";
string postData = "param1=1¶m2=2";
byte[] data = new ASCIIEncoding().GetBytes(postData);
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
stream.Close();
request.GetResponse().Close(); // This line gets a 401 Not Authorized error.
}
编辑:有人建议我需要传输cookie。以下也不起作用:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("my-server");
request.Credentials = new NetworkCredential("user", "password");
var response = (HttpWebResponse)request.GetResponse();
var cookieContainer = new CookieContainer();
foreach (Cookie cookie in response.Cookies)
{
cookieContainer.Add(cookie);
}
response.Close();
// Now the request I want to make...
request = (HttpWebRequest)WebRequest.Create("my-server");
request.Credentials = new NetworkCredential("user", "password");
request.CookieContainer = cookieContainer;
request.Method = "post";
string postData = "param1=1¶m2=2";
byte[] data = new ASCIIEncoding().GetBytes(postData);
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
request.GetResponse().Close(); // This line gets a 401 Not Authorized error.
}
I need to make a POST request using .Net.
I can authenticate by GET, and so I’m trying to make a POST request on the same connection to keep my authentication.
The problem is I get a 401 Not Authenticated exception, which implies the connection hasn’t been reused.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("my-server");
request.Credentials = new NetworkCredential("user", "password");
request.GetResponse().Close(); // Works fine
// Now the request I want to make...
request = (HttpWebRequest)WebRequest.Create("my-server");
request.Credentials = new NetworkCredential("user", "password");
request.Method = "post";
string postData = "param1=1¶m2=2";
byte[] data = new ASCIIEncoding().GetBytes(postData);
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
stream.Close();
request.GetResponse().Close(); // This line gets a 401 Not Authorized error.
}
EDIT: There has been some suggestions that I need to transfer cookies. The following doesn’t work either:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("my-server");
request.Credentials = new NetworkCredential("user", "password");
var response = (HttpWebResponse)request.GetResponse();
var cookieContainer = new CookieContainer();
foreach (Cookie cookie in response.Cookies)
{
cookieContainer.Add(cookie);
}
response.Close();
// Now the request I want to make...
request = (HttpWebRequest)WebRequest.Create("my-server");
request.Credentials = new NetworkCredential("user", "password");
request.CookieContainer = cookieContainer;
request.Method = "post";
string postData = "param1=1¶m2=2";
byte[] data = new ASCIIEncoding().GetBytes(postData);
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
request.GetResponse().Close(); // This line gets a 401 Not Authorized error.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不保持登录状态的原因是您没有为 HttpWebRequest 提供 CookieContainer 来保存会话 id。
请参阅以下 StackOverflow 问答以获取可能的解决方案:
C# 通过 httpwebrequest 保留会话 ID
同一会话中的多个 WebRequest
我希望这会有所帮助。
The reason you don't stay logged in is because you don't give the HttpWebRequest a CookieContainer to keep the session id in.
See the following to StackOverflow Q&A's for your possible solution:
C# keep session id over httpwebrequest
Multiple WebRequest in same session
I hope this helped.
知道另一端如何管理身份验证吗? IE。如果它设置了 cookie,那么您需要确保对此进行解释,请参阅此 页面,特别是此注释:
Any idea how the authentication is managed on the other end? IE. If it sets a cookie, then you need to make sure you account for that, see this page, and specifically this note: