【问题标题】:http response code for SSL sites aspSSL站点asp的http响应代码
【发布时间】:2011-10-18 09:51:05
【问题描述】:

我正在尝试创建一个 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 中的新手,任何帮助表示赞赏。

【问题讨论】:

  • 您确实在 url 中指定了“https”,不是吗?当您单步执行代码(使用调试器)时会看到什么?响应中的内容是什么?
  • 证书被接受,但httpres的值仍然设置为null。

标签: c# .net https httpresponse


【解决方案1】:

我发现了问题,https 响应为 200(OK),而 http 响应为 302(Found),所以我只是修改了我的条件,将其添加 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();
       }

问候, 普拉桑特

【讨论】:

    猜你喜欢
    • 2020-11-12
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多