我正在尝试创建一个Windows应用程序,以便在网站关闭时发送警报,我首先编写这个基本表单以检查它是否正常工作。
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(textBox1.Text);
httpReq.AllowAutoRedirect = false;
HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
if (httpRes.StatusCode == HttpStatusCode.Found)
{
MessageBox.Show("It works.");
}
else
{
MessageBox.Show("Not able to ping");
}
httpRes.Close();
它工作正常,但当我想为SSL网站(https)做同样的事情时,它没有用,我查了一下并添加了
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
但仍然无法从https网站得到任何回复,我尝试了很多网站,所以我不认为它的网站问题,我是一个新手.net任何帮助表示赞赏。
答案 0 :(得分:0)
我发现问题的人,https用200响应(OK)而http正在响应302(找到)所以我只是修改了我的条件为它添加200它就像一个魅力。对于任何对工作代码感兴趣的人,这里是
public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(textBox1.Text);
httpReq.AllowAutoRedirect = false;
HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
if (httpRes.StatusCode == HttpStatusCode.OK || httpRes.StatusCode==HttpStatusCode.Found)
{
MessageBox.Show("It works.");
}
else
{
MessageBox.Show("Not able to ping");
}
httpRes.Close();
}
此致 Prasanth