C#HTTPS访问

博客提及主要参考链接https://blog.youkuaiyun.com/c1149884598/article/details/86494735,介绍了一个WPF工程,该工程是为井盖项目服务的,同时提醒代码中有关URL的字符串已隐藏,需更改后才能执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要参考
https://blog.youkuaiyun.com/c1149884598/article/details/86494735

wpf工程,为井盖那个项目服务

注意,代码中有关url的字符串已经隐藏,需更改后才能执行

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HTTPS
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://xxx.xxx.xxx.xxx:8080");
            HttpWebResponse response = (HttpWebResponse)myReq.GetResponse();
            Stream ResponseStream = response.GetResponseStream();
            StreamReader StreamReader = new StreamReader(ResponseStream, Encoding.UTF8);
            string ResponseInfo = StreamReader.ReadToEnd();

            System.Diagnostics.Debug.WriteLine("asddsf");


            string rev = "";
            HTTPS_aaa.HttpRequest("GET", "https://xxx.xxx.xxx.xxx:443", null, ref rev, 200);
            System.Diagnostics.Debug.WriteLine("asddsf");



            string rev1 = "";
            HTTPS_aaa.HttpRequest("POST", "https://xxx.xxx.xxx.xxx:xxxx/iocm/app/sec/v1.1.0/login", "appId=msio5OKhdUXfSw9WnuDyWBRHKW4a&secret=LvCKmM949ekDvrQgbaYFqNFsVxwa", ref rev1, 200);
            System.Diagnostics.Debug.WriteLine("asddsf");
        }
    }


    public static class HTTPS_aaa
    {
        // 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)
        {
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertificateValidate);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            X509Certificate2 cer = new X509Certificate2(@"./HTTPS/outgoing.CertwithKey.pkcs12", "IoM@1234");
            //                X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            //                store.Open(OpenFlags.MaxAllowed);
            //                store.Remove(cer);
            //                store.Add(cer);
            //                store.Close();

            request.ClientCertificates.Add(cer);
            request.Method = GetOrPost;
            request.KeepAlive = false;

            request.AllowAutoRedirect = false;
            request.Timeout = TimeOutMs;
            // request.ContentType =  "application/x-www-form-urlencoded;charset=utf-8";


            //3.在WebRequest请求之前调用:
            ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;

            if (GetOrPost == "POST")
            {
                int RequestLength = Encoding.UTF8.GetByteCount(RequestInfo);
                request.ContentLength = RequestLength;
                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;
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值