///
///发送SSL加密请求///
///
///
///
///
///
///
public static string PostDataBySSL(string post_data, string url, string cert_path, string cert_password, out stringerrInfo)
{
errInfo= string.Empty;try{
ASCIIEncoding encoding= newASCIIEncoding();byte[] data =encoding.GetBytes(post_data);if (cert_path != string.Empty)
{//验证证书,默认有效
ServicePointManager.ServerCertificateValidationCallback = newRemoteCertificateValidationCallback(ValidateServerCertificate);
}//获取结果
WebRequest webRequest =WebRequest.Create(url);//配置请求参数
HttpWebRequest httpRequest = webRequest asHttpWebRequest;if (cert_path.ToLower().EndsWith(".cer"))
{
httpRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile(cert_path));
}else{var s = newX509Certificate2(cert_path, cert_password);
httpRequest.ClientCertificates.Add(s);//httpRequest.ClientCertificates.Add(new X509Certificate2(cert_path, cert_password));//X509Store store = new X509Store("My", StoreLocation.LocalMachine);//store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);//System.Security.Cryptography.X509Certificates.X509Certificate2 cert =//store.Certificates.Find(X509FindType.FindBySubjectName, "zzjy001.e.3002", false)[0];
}//获取或设置一个值,该值指示是否与 Internet 资源建立持久性连接。
httpRequest.KeepAlive = true;//获取或设置 User-agentHTTP 标头的值。
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)";
httpRequest.ContentType= "application/x-www-form-urlencoded";
httpRequest.Method= "POST";//发送 POST 的 BODY数据 要发送到 Internet 资源的数据的字节数
httpRequest.ContentLength =data.Length;
Stream requestStream= httpRequest.GetRequestStream(); //获取用于写入请求数据的 Stream 对象
requestStream.Write(data, 0, data.Length); //发送带有 HttpWebRequest 的数据
requestStream.Close();
Stream responseStream= null; //从 Internet 资源返回数据流//获取HTTPs的响应
responseStream =httpRequest.GetResponse().GetResponseStream();string stringResponse = string.Empty;if (responseStream != null)
{using (StreamReader responseReader =
new StreamReader(responseStream, Encoding.GetEncoding("GBK")))
{
stringResponse=responseReader.ReadToEnd();
}
responseStream.Close();
}returnstringResponse;
}catch(Exception e)
{
errInfo=e.Message;var result =SpringFactory.BusinessFactory.GetBusinessAnonymousUser();
result.AddLogs("返回2:" +e.Message);return string.Empty;
}
}