我正嘗試使用httpwebrequest登錄到網站並在webbrowser中顯示響應。使用httpwebrequest登錄網站並在瀏覽器中顯示響應
這是我的代碼。
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登錄網站。