本文介绍了HttpWebRequest的在C#中的https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这一块code不工作;它登录到网站,使用https协议。如何解决这个问题呢?在code停在 GetRequestStream()
随时随地说,违反协议的例外是未处理。
字符串的用户名=用户;
字符串密码=通行证;
HttpWebRequest的要求= (HttpWebRequest)WebRequest.Create("https://moje.azet.sk/prihlasenie.phtml?KDE=www.azet.sk%2Findex.phtml%3F");
request.UserAgent =Mozilla的/ 4.0(兼容; MSIE 6.0; Windows NT的5.1; .NET CLR 1.0.3705);
Console.WriteLine(request.GetRequestStream());
使用(StreamWriter的作家=新的StreamWriter(request.GetRequestStream(),Encoding.ASCII))
{
writer.Write(尼克=+用户名+&放大器;密码=+密码);
}
HttpWebResponse响应=(HttpWebResponse)request.GetResponse();
//检索你的cookie的ID的会话
//response.Cookies
使用(StreamReader的读者=新的StreamReader(response.GetResponseStream()))
{
Console.WriteLine(reader.ReadToEnd());
}
解决方案
发布设置请求方法,调用GetRequestStream之前
喜欢
request.Method =POST;
使用(StreamWriter的作家=新的StreamWriter(request.GetRequestStream(),Encoding.ASCII))
{
writer.Write(尼克=+用户名+&放大器;密码=+密码);
}
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());
}
解决方案
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);
}
这篇关于HttpWebRequest的在C#中的https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!