2011-10-18 55 views
1

我想創建一個Windows應用程序發送警報,如果一個網站關閉,我首先寫了這個基本的形式來檢查它是否工作。http響應代碼SSL網站asp

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

你在URL中指定「HTTPS」,不是嗎?當你逐步執行代碼(使用調試器)時,會看到什麼?響應中有什麼? –

+0

證書被接受,但httpres的值仍然設置爲空。 – prasanth

回答

0

我發現問題的傢伙,HTTPS的響應與200(確定),而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