• 转的 winform开发连接webservice中单向证书


    http://hi.baidu.com/srxljl/blog/item/8849a6ef4304961efdfa3cd3.html

    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;
         }

       }
    }

     

     

    如果是 webservice调用:

     

     

     HttpsService.LoginService httpsls = new Addin.HostDemo.HttpsService.LoginService();

                    httpsls.ClientCertificates.Add(
                       X509Certificate.CreateFromCertFile(AppDomain.CurrentDomain.BaseDirectory + "CaresCA_ServerGrp_CA.cer"));
                    ServicePointManager.ServerCertificateValidationCallback =
                            new RemoteCertificateValidationCallback(
                                 (a, b, c, d) => { return true; }
                            );

                    
                    httpsls.login("test1", "123456");

  • 相关阅读:
    [恢]hdu 1412
    [恢]hdu 2212
    [恢]hdu 1407
    [恢]hdu 1228
    [恢]hdu 1337
    [恢]hdu 1014
    [恢]hdu 2200
    定居到博客园了!
    比较GridView,DataList,Repeator ,DetailsView,FormView
    随手记录修改某条记录时,不使用数据库控件而用datareader
  • 原文地址:https://www.cnblogs.com/neverlost/p/1739989.html
Copyright © 2020-2023  润新知