【问题标题】:HttpWebRequest, c# and HttpsHttpWebRequest、c# 和 Https
【发布时间】:2012-01-07 01:19:20
【问题描述】:

我尝试了许多以编程方式登录 https 网站的方法,但我遇到了问题。每次我收到一条错误消息,指出我的登录名和密码不正确。我确信它们是正确的,因为我可以使用相同的凭据通过浏览器登录该站点。

失败代码

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://www.majesticseo.com/account/login?EmailAddress=myemail&Password=mypass&RememberMe=1");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,**;q=0.8";
request.UnsafeAuthenticatedConnectionSharing = true;
request.Method = "POST";
request.KeepAlive = true;
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = true;
request.CookieContainer = container;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

//String tmp;
foreach(Cookie cookie1 in response.Cookies)
{
    container.Add(cookie1);
}

Stream stream = response.GetResponseStream();

string html = new StreamReader(stream).ReadToEnd();
Console.WriteLine("" + html);

【问题讨论】:

  • 您在代码中的哪个位置设置凭据?所有注释掉的代码的目的是什么?它与问题有关吗?如果没有,请删除它。
  • 很明显你没有做 BASIC auth,所以我不能告诉你需要设置哪些字段,因为我不知道这个网站是什么。另外请格式化和清理这段代码,它目前的形式非常令人困惑。

标签: c# ssl https httpwebrequest


【解决方案1】:

该站点使用 HTTP POST 进行登录,并且不会在 URL 中发送用户名和密码。

正确的登录网址是https://www.majesticseo.com/account/login

您需要创建要发布的数据字符串,将其转换为字节数组,设置内容长度,然后执行您的请求。发送内容长度非常重要。没有它,帖子将无法正常工作。

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://www.majesticseo.com/account/login?EmailAddress=myemail&Password=mypass&RememberMe=1");

        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0";
        request.Referer = "https://www.majesticseo.com/account/login";
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,**;q=0.8";
        request.UnsafeAuthenticatedConnectionSharing = true;
        request.Method = "POST";
        request.KeepAlive = true;
        request.ContentType = "application/x-www-form-urlencoded";
        request.AllowAutoRedirect = true;

        // the post string for login form
        string postData = "redirect=&EmailAddress=EMAIL&Password=PASS";
        byte[] postBytes = System.Text.Encoding.ASCII.GetBytes(postData);

        request.ContentLength = postBytes.Length;

        System.IO.Stream str = request.GetRequestStream();

        str.Write(postBytes, 0, postBytes.Length);

        str.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        System.IO.Stream stream = response.GetResponseStream();


        string html = new System.IO.StreamReader(stream).ReadToEnd();

        Console.WriteLine("" + html);

【讨论】:

    【解决方案2】:

    您正在尝试发布某些内容(我从您的代码中看不到,什么),但不是凭据。我猜您的网页会显示一个网络表单,您可以在其中输入用户名(电子邮件地址?)和密码。然后浏览器发布此表单。因此,您需要复制浏览器行为 - 对表单内容进行编码并在您的发布请求中发送它们。使用一些流行浏览器的网站管理员开发工具来查看客户端浏览器向服务器发送的确切内容以及它如何编码表单数据。接下来,您的请求很可能需要特殊的 cookie,您可以通过访问另一个页面(例如登录页面)来收集这些 cookie。发送预设 cookie(就像您在注释代码中所做的那样)不适用于大多数网站。

    换句话说,正确的机制是:

    1. 获取登录网页
    2. 收集 cookie
    3. POST 表单数据并在请求中传递收集的 cookie。
    4. 收集其他cookie,这些cookie可能在登录后发送。

    【讨论】:

    • 我尝试过这种方式,但这不起作用。第一个解决方案正在工作(将登录数据作为字节数组发送)。谢谢。
    • @jars 好吧,如果不调试您的代码并检查您的特定站点,我认为没有人可以在这里为您提供帮助。所以你在这里不走运。
    猜你喜欢
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    相关资源
    最近更新 更多