⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Security.Cryptography.X509Certificates;

namespace SelfSignedSSLCert
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// TODO: Change URL to address on your own server
        /// </summary>
        private readonly string webServiceURL = "https://dev/SecureService/Service.asmx";

        public Form1()
        {
            InitializeComponent();
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CallSecureWebService()
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                SecureWebService.Service websvc = new SelfSignedSSLCert.SecureWebService.Service();
                websvc.Url = webServiceURL;
                string response = websvc.HelloSecureWorld();
                label1.Text = response;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception");
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void menuItemCallWithCallback_Click(object sender, EventArgs e)
        {
            System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
            CallSecureWebService();
        }

        private void menuItemCallNoCallback_Click(object sender, EventArgs e)
        {
            System.Net.ServicePointManager.CertificatePolicy = null;
            CallSecureWebService();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.Assert(false, "Have you installed the SecureService Web Service from the sample code, and changed the webServiceURL field in this class to the correct URL? When you have, comment out this Assert in Form1_Load.");
        }

        private void menuItemCallService_Click(object sender, EventArgs e)
        {

        }
    }

    public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
    {
        public TrustAllCertificatePolicy()
        { }

        public bool CheckValidationResult(ServicePoint sp,
         X509Certificate cert, WebRequest req, int problem)
        {
            // TODO: return true to trust all server certificates
            return true;
            // TODO: Alternatively, edit Company name and Host name in code below, and uncomment
            // to only accept specific server certificates
            //int UnTrustedRoot = (int)(0x800B010D & 0x000FFFFF);  
            //int UnTrustedCA = (int)(0x800B0112 & 0x000FFFFF);
            //if (certificateProblem == UnTrustedRoot || certificateProblem == UnTrustedCA)
            //{
            //    if (certificate.GetIssuerName().Equals("MyCompany") && request.RequestUri.Host.Equals("MyServer"))
            //        return true;
            //}
            //return false;

        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -