当前位置:首页 > 后端开发 > c#中https通讯如何添加证书

c#中https通讯如何添加证书

6个月前 (05-24)51

c#如何在https在通讯中需要添加证书密码

public static class HTTPS
    {
       // byte[] certificate = Properties.Resources.client1;
         //2.定义方法:
        private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
        {
            //为了通过证书验证,总是返回true
            return true;
        }
        public static int HttpRequest(string GetOrPost, string Url, string RequestInfo, ref string ResponseInfo, int TimeOutMs)
        {
            try
            {
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertificateValidate);
              //  CookieContainer CookieContainer = new CookieContainer();
              
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                X509Certificate2 cer = new X509Certificate2(@"./cl1.p12", "123");
                X509Store store = new X509Store(StoreName.My,StoreLocation.LocalMachine);
                store.Open(OpenFlags.MaxAllowed);
                store.Remove(cer);
                store.Add(cer);
                store.Close();
               // request.ProtocolVersion = HttpVersion.Version11;
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
                //ServicePointManager.Expect100Continue = false;
                request.ClientCertificates.Add(cer);
                request.Method = GetOrPost;
                request.KeepAlive = false;
               // request.CookieContainer = CookieContainer;
                request.AllowAutoRedirect = false;
                request.Timeout = TimeOutMs;
               // request.ContentType =  "application/x-www-form-urlencoded;charset=utf-8";

                int RequestLength = Encoding.UTF8.GetByteCount(RequestInfo);
                request.ContentLength = RequestLength;

                //3.在WebRequest请求之前调用:
                ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
                
                if (GetOrPost == "POST")
                {
                    Stream RequestStream = request.GetRequestStream();
                    RequestStream.Write(Encoding.UTF8.GetBytes(RequestInfo), 0, RequestLength);
                    RequestStream.Close();
                    
                }

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream ResponseStream = response.GetResponseStream();
                StreamReader StreamReader = new StreamReader(ResponseStream, Encoding.UTF8);
                ResponseInfo = StreamReader.ReadToEnd();
                StreamReader.Close();
                response.Close();

                return 0;
            }
            catch (Exception ex)
            {
                ResponseInfo = "HTTP通讯异常:" + ex.Message;
                return -1;
            }
        }
    }

以上代码写的很乱,包含了许多不用的设置,仅供参考。

作者:_蓝染_
来源链接:https://blog.csdn.net/c1149884598/article/details/86494735

标签: HTTPS

“c#中https通讯如何添加证书” 的相关文章

HTTPS工作原理

HTTPS工作原理

HTTPS是什么 HTTPS全称为Hypertext Transfer Protocol over Secure Socket Layer,及以安全为目标的HTTP通道,简单说就是...

华三交换机开启telnet ssh https登陆方式

华三交换机开启telnet ssh https登陆方式 1、开启telnet,ssh,https登陆方式 [H3C]telent ser...

HTTPS协议详解(一):HTTPS基础知识

HTTPS协议详解(一):HTTPS基础知识

本文大部分内容摘自: http://www.wosign.com/faq/faq2016-0309-01.htm 尊重知识产权, 转载注明 Wos...

HTTP及HTTPS简介

HTTP及HTTPS简介

背景 HTTP历史以及HTTPS的升级过程 前言 先复习下OSI(Open System Interconnect) 参考模型 和...

关于启用 HTTPS 的一些经验分享(一)

关于启用 HTTPS 的一些经验分享(一)

转载: 关于启用 HTTPS 的一些经验分享(一) 随着国内网络环境的持续恶化,各种篡改和劫持层出不穷,越来越多的网站选择了全站 HTTPS。就在今天,免费提供证...

手机安装Charles的https证书

手机安装Charles的https证书 详情描述 一、iOS客户端安装证书 打开Charles,选择help→SSL Pr...

浅谈HTTP和HTTPS的区别

这篇随笔我们从六个步骤来学习:   1、了解HTTP和HTTPS的基本概念   2、HTTPS诞生的目的   3、HTTP与HTTPS的区别   4、HTTP和H...

HTTP和HTTPS协议

HTTP和HTTPS协议

介绍 http[1]是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是一种应用层协议,可用于将超文本服务器中文本、图片、音视频等内容传输到客户端...

vue开启https,加载本地证书

vue开启https,加载本地证书

vue开启https,加载本地证书 1. 通过mkcert创建本地证书 1.1 安装mkcert 安装方式:npm...

可以使用ip地址+https进行访问吗?

可以使用ip地址+https进行访问吗? 答案是:常规情况下不可以。 为什么呢? 因为https申请的证书是和域名进行绑定的,直接使用h...