HttpWebRequest 与 C# 中的 https [英] HttpWebRequest with https in C#

查看:34
本文介绍了HttpWebRequest 与 C# 中的 https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

在调用GetRequestStream之前设置请求方法为post

Set request method to post, before calling GetRequestStream

喜欢

request.Method = "POST";

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

这篇关于HttpWebRequest 与 C# 中的 https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆