C# 中带有 https 的 HttpWebRequest
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/542024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpWebRequest with https in C#
提问by Skuta
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..
这段代码不起作用;它正在登录使用 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());
}
采纳答案by Ramesh
Set request method to post, before calling GetRequestStream
在调用 GetRequestStream 之前将请求方法设置为 post
like
喜欢
request.Method = "POST";
using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
{
writer.Write("nick=" + username + "&password=" + password);
}
回答by Rytmis
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:
您可能还想事先弄清楚您发布的内容的总长度,并将其设置为请求的 ContentLength。见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 propertyor the SendChunked is false when buffering is disabled and on a keepalive connection (the KeepAlive property is true).
在 HttpWebRequest 类上设置的属性发生冲突的几种情况下,会引发 ProtocolViolationException。如果应用程序将 ContentLength 属性和 SendChunked 属性设置为 true,然后发送 HTTP GET 请求,则会发生此异常。如果应用程序尝试将分块发送到仅支持 HTTP 1.0 协议的服务器,则会发生此异常,而该协议不受支持。如果应用程序尝试在不设置 ContentLength 属性的情况下发送数据,或者在禁用缓冲和保持活动连接(KeepAlive 属性为 true)时 SendChunked 为 false,则会发生此异常。
回答by regex
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.
我的猜测是您遇到的问题是由于您正在执行 GET 请求而不是 POST 请求这一事实(就像其他人所建议的那样)。此外,我注意到该页面上密码字段的实际名称是“heslo”而不是“password”。这种打字错误不会导致 Web 服务器不返回响应,但会导致其他问题,因为服务器正在寻找与密码值一起发布的特定变量名称。
回答by Schultz9999
Here is what works for me:
以下是对我有用的方法:
var request = WebRequest.Create(_url);
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential(_userName, _password);