C# 中带有 https 的 HttpWebRequest

发布于 2024-07-13 12:08:32 字数 885 浏览 4 评论 0原文

这段代码不起作用; 它正在登录使用 https 协议的网站。 如何解决这个问题呢? 该代码随时随地停止在 GetRequestStream() 处,表示协议违反异常未处理。

string username = "user";
string password = "pass";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://moje.azet.sk/prihlasenie.phtml?KDE=www.azet.sk%2Findex.phtml%3F");
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)";

Console.WriteLine(request.GetRequestStream());

using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
{
    writer.Write("nick=" + username + "&password=" + password);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//Retrieve your cookie that id's your session
//response.Cookies

using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    Console.WriteLine(reader.ReadToEnd());
}

This piece of code doesn't work; it's logging in into website which is using https protocol. How to solve this problem? The code stops at GetRequestStream() anytime anywhere saying that protocol violation exception is unhandled..

string username = "user";
string password = "pass";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://moje.azet.sk/prihlasenie.phtml?KDE=www.azet.sk%2Findex.phtml%3F");
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)";

Console.WriteLine(request.GetRequestStream());

using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
{
    writer.Write("nick=" + username + "&password=" + password);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//Retrieve your cookie that id's your session
//response.Cookies

using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    Console.WriteLine(reader.ReadToEnd());
}

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

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

发布评论

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

评论(4

那片花海 2024-07-20 12:08:32

将请求方法设置为 post

在调用 GetRequestStream之前

request.Method = "POST";

using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
{
    writer.Write("nick=" + username + "&password=" + password);
}

Set request method to post, before calling GetRequestStream

like

request.Method = "POST";

using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
{
    writer.Write("nick=" + username + "&password=" + password);
}
若有似无的小暗淡 2024-07-20 12:08:32

我的猜测是,您遇到的问题是由于您正在执行 GET 请求而不是 POST 请求(就像其他人建议的那样)。 此外,我注意到该页面上密码字段的实际名称是“heslo”而不是“password”。 此拼写错误不会导致 Web 服务器不返回响应,但会导致其他问题,因为服务器正在查找要与密码值一起发布的特定变量名称。

My guess is that the issue you are experiencing is due to the fact (like others have advised) that you are doing a GET request instead of a POST request. Additionally, I noticed that the actual name for the password field on that page is "heslo" and not "password". This typo won't cause the web server to not return a response, but it will cause other issues since the server is looking for that specific variable name to be posted with the password value.

酒绊 2024-07-20 12:08:32

您可能还想事先计算出要发布的内容的总长度,并将其设置为请求的 ContentLength。 请参阅 MSDN

当在协议上设置属性时,在多种情况下会抛出 ProtocolViolationException。
HttpWebRequest 类存在冲突。 如果应用程序设置了此异常,则会发生此异常
ContentLength 属性和 SendChunked 属性设置为 true,然后发送 HTTP GET
要求。 如果应用程序尝试将分块发送到服务器,则会发生此异常
仅支持 HTTP 1.0 协议,不支持此协议。 如果发生此异常
应用程序尝试在不设置 ContentLength 属性或
当禁用缓冲并且在保持活动连接(
KeepAlive 属性为 true)。


You might also want to figure out the total length of what you're posting, beforehand, and set that as the ContentLength of the request. See MSDN:

A ProtocolViolationException is thrown in several cases when the properties set on the
HttpWebRequest class are conflicting. This exception occurs if an application sets the
ContentLength property and the SendChunked property to true, and then sends an HTTP GET
request. This exception occurs if an application tries to send chunked to a server that
only supports HTTP 1.0 protocol, where this is not supported. This exception occurs if an
application tries to send data without setting the ContentLength property
or the
SendChunked is false when buffering is disabled and on a keepalive connection (the
KeepAlive property is true).

心的位置 2024-07-20 12:08:32

这对我有用:

    var request = WebRequest.Create(_url);
    request.PreAuthenticate = true;
    request.Credentials = new NetworkCredential(_userName, _password);

Here is what works for me:

    var request = WebRequest.Create(_url);
    request.PreAuthenticate = true;
    request.Credentials = new NetworkCredential(_userName, _password);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文