1

I am trying to login to a website with httpwebrequest and show the response in webbrowser.

here is my code.

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

I am getting this error:

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

I am unable to figure out what's wrong with my code. I want to login to a website with HttpWebRequest.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
sauk
  • 55
  • 8

1 Answers1

2

If you browse to that site in Chrome, you get

This is probably not the site you are looking for! You attempted to reach mig33.com, but instead you actually reached a server identifying itself as www.mig33.com. This may be caused by a misconfiguration on the server or by something more serious. An attacker on your network could be trying to get you to visit a fake (and potentially harmful) version of mig33.com.

The security certificate is not valid, because it has only been configured for www.mig33.com, not mig33.com.

If you change the URL to https://www.mig33.com it should work.

In the more general case of an invalid certificate, if you must proceed in spite of the invalid SSL cert, have a look at

How to ignore the certificate check when ssl

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553