使用httpwebrequest登录网站并在webbrowser中显示响应

时间:2014-04-30 21:18:51

标签: c# .net httpwebrequest browser

我正在尝试使用httpwebrequest登录网站并在webbrowser中显示响应。

这是我的代码。

public void getContent()
        {
CookieCollection cookies = new CookieCollection();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://mig33.com");
            request.CookieContainer = new CookieContainer();
            request.CookieContainer.Add(cookies);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            cookies = response.Cookies;

             string getUrl = "https://mig33.com";
             string postData = String.Format("email={0}&pass={1}", "username", "password");
             HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
             getRequest.CookieContainer = new CookieContainer();
             getRequest.CookieContainer.Add(cookies); //recover cookies First request
             getRequest.Method = WebRequestMethods.Http.Post;
             getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
             getRequest.AllowWriteStreamBuffering = true;
             getRequest.ProtocolVersion = HttpVersion.Version11;
             getRequest.AllowAutoRedirect = true;
             getRequest.ContentType = "application/x-www-form-urlencoded";

             byte[] byteArray = Encoding.ASCII.GetBytes(postData);
             getRequest.ContentLength = byteArray.Length;   
             Stream newStream = getRequest.GetRequestStream(); 
             newStream.Write(byteArray, 0, byteArray.Length); 
             newStream.Close();

             HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
             using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
             {
                 //webBrowser1.Navigate("https://mig33.com", "", byteArray, "Content-Type: application/x-www-form-urlencoded");
               string sourceCode = sr.ReadToEnd();               
             } 
                        webBrowser1.Navigate("https://mig33.com", "",byteArray, "Content-Type: application/x-www-form-urlencoded");
                    }

        private void button1_Click(object sender, EventArgs e)
        {
            getContent();
        }
    }
}

我收到此错误:

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

我无法弄清楚我的代码有什么问题。我想使用HttpWebRequest登录网站。

1 个答案:

答案 0 :(得分:2)

如果您在Chrome浏览器中浏览该网站,则会获得

  

这可能不是您要找的网站!   您试图访问mig33.com,但实际上您已经到达了自称为www.mig33.com的服务器。这可能是由服务器上的配置错误或更严重的问题引起的。您网络上的攻击者可能会试图让您访问mig33.com的假(且可能有害)版本。

安全证书无效,因为它仅配置为www.mig33.com,而不是mig33.com

如果您将网址更改为https://www.mig33.com,则该网址应该有效。

在更一般的无效证书的情况下,如果您必须尽管SSL证书无效,请查看

How to ignore the certificate check when ssl