双向验证证书地址请求方式

url:请求地址
value:请求参数
cerPath:P12证书路径
password:证书密码
public static HttpResult PostRequest( string url, object value, string certPath, string password)
{
try
{
var parameters = JsonWrapper.SerializeObject(value);
byte[] data = Encoding.UTF8.GetBytes(parameters);
var request = (HttpWebRequest)WebRequest.Create(url);
request.ContentLength = data.Length;
request.Headers.Add(“cache-control”, “no-cache”);
ServicePointManager.ServerCertificateValidationCallback
= new RemoteCertificateValidationCallback(CheckValidationResult);
X509Certificate cert = new X509Certificate(certPath, password, X509KeyStorageFlags.MachineKeySet);
request.ClientCertificates.Add(cert);
request.Method = “POST”;
request.ContentType = “application/json”;
var stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
return new HttpResult
{
StatusCode = response.StatusCode,
Data = JsonWrapper.DeserializeObject(content)
};
}
catch (System.Exception e)
{
throw e;
}
}

你可能感兴趣的:(双向验证证书地址请求方式)