金鹏

|| 叶落知秋,见微知著。|| 不出户,知天下﹔不窥牖,见天道。其出弥远,其知弥少。是以圣人不行而知,不见而明,不为而成。

博客园 首页 新随笔 联系 订阅 管理

ServicePointManager.ServerCertificateValidationCallback 属性
ms-help://MS.MSDNQTR.v90.chs/fxref_system/html/dcd4157d-dba4-4b60-164a-9e433a045d3c.htm

RemoteCertificateValidationCallback 委托

ms-help://MS.MSDNQTR.v90.chs/fxref_system/html/b3a0c706-1033-a543-01ca-23d09fcc121a.htm

WebRequest and SSL (The underlying connection was closed. Could not establish trust relationship with remote server.)
http://weblogs.asp.net/wim/archive/2004/04/02/106281.aspx

WinForm 调 SSL VPN WebService
http://www.cnblogs.com/zhongzf/archive/2006/10/27/386902.html

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Net;
using System.Collections;
using System.Threading;

using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace GetCWData {
  
public partial class Form1 : Form {

    CookieContainer cc;

    
public Form1() {
      InitializeComponent();

      BeginGetData();
    }

    
private void BeginGetData() {
      
this.cc = new CookieContainer();

      
this.txtUsername.Text = GetCWData.Properties.Settings.Default.Username;
      
this.txtPassword.Text = GetCWData.Properties.Settings.Default.Password;
      
string url = GetCWData.Properties.Settings.Default.BaseURL + GetCWData.Properties.Settings.Default.LoginPage;

      
//ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
      ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);

      HttpWebRequest request 
= (HttpWebRequest)WebRequest.Create(url);
      request.Method 
= "POST";
      request.ContentType 
= "application/x-www-form-urlencoded";
      request.ContentLength 
= 0;
      request.UserAgent 
= "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.1124)";
      request.CookieContainer 
= this.cc;

      HttpWebResponse response 
= (HttpWebResponse)request.GetResponse();

      Stream stream 
= response.GetResponseStream();
      StreamReader reader 
= new StreamReader(stream, Encoding.UTF8);
      
string result = reader.ReadToEnd();

      
this.txtHTML.Text = result;
    }

    
//internal class AcceptAllCertificatePolicy : ICertificatePolicy {
    
//  public AcceptAllCertificatePolicy() {
    
//  }

    
//  public bool CheckValidationResult(ServicePoint sPoint, X509Certificate cert, WebRequest wRequest, int certProb) {
    
//    // Always accept
    
//    return true;
    
//  }
    
//}

    
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
      
//if (sslPolicyErrors == SslPolicyErrors.None) {
      
//  return true;
      
//}

      
//Console.WriteLine("Certificate error: {0}", sslPolicyErrors);

      
//// Do not allow this client to communicate with unauthenticated servers.
      //return false;

      
return true;
    }

  }
}
posted on 2008-02-27 11:18  金鹏  阅读(1945)  评论(1编辑  收藏  举报