在 C# 中重用 HttpWebRequest 的连接

发布于 2024-12-29 08:12:26 字数 1941 浏览 1 评论 0原文

我需要使用 .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&param2=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&param2=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 技术交流群。

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

发布评论

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

评论(2

笑咖 2025-01-05 08:12:26

您不保持登录状态的原因是您没有为 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.

韬韬不绝 2025-01-05 08:12:26

知道另一端如何管理身份验证吗? IE。如果它设置了 cookie,那么您需要确保对此进行解释,请参阅此 页面,特别是此注释:

注意

出于安全原因,默认情况下禁用 cookie。如果您想使用 cookie,请使用 CookieContainer 属性启用 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:

Note

For security reasons, cookies are disabled by default. If you want to use cookies, use the CookieContainer property to enable cookies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文