HttpWebRequest的在C#中的httpsHttpWebRequest、https

2023-09-02 02:08:23 作者:浅末年代  ゅˉ 7c

这一块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(尼克=+用户名+&放大器;密码=+密码);
}
 
C 使用HttpWebRequest 实现简单的get和post请求

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);
}