工商银行缴费页面找不到服务器,工行在线支付接口问题

这段代码展示了如何在C#中通过SSL进行POST请求,包括加载证书、设置请求头和发送数据的步骤。它还包含错误处理和证书验证回调函数。

///

///发送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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值