主要参考
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;
}
}
}