带有 .crt 和 .key 的 HTTPS Post
Https Post with .crt and .key
很抱歉,如果重复了。我在互联网上看到几个 post 关于这个话题。但是没有得到任何合适的解决方案。我在 nopcommerce 4.0 工作,它在 .net 核心上是 运行,但目标框架是 4.x.x。我正在开发一个需要与 .crt 和 .key 文件安全连接的支付插件。付款方式提供商发送了一个按预期工作的 php 文件。下面是示例代码
function ProcessRequest($curl_post_data,$service_url,$proxy,$proxyauth)
{
$output = '';
$certfile = '/createorder.crt';
$keyfile = '/createorder.key';
$cert_password = '';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $service_url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_SSLCERT, getcwd() . $certfile );
curl_setopt( $ch, CURLOPT_SSLKEY, getcwd() . $keyfile );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$output = curl_exec($ch);
if (curl_error($ch)) {
echo $error_msg = curl_error($ch);
}
$cblcz = json_decode($output, true );
return $cblcz;
}
$proxy ="";
$proxyauth ="";
$postDatatoken = '{
"password": "123456Aa",
"userName": "test"
}';
$serviceUrltoken ="";
$serviceUrltoken= 'https://sandbox.thecitybank.com:7788/transaction/token';
$cblcz = ProcessRequest($postDatatoken,$serviceUrltoken,$proxy,$proxyauth);
我无法将此 curl 转换为 http post。我在下面试过 link
和其他人,但没有解决任何问题。
不工作。对于 openssl,它抛出依赖项未加载。这是我的 C# 代码
try
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
var certPath = Path.Combine(CommonHelper.MapPath("~/Plugins/Payments.CityBankApi/"), "createorder.crt");
var keyPath = Path.Combine(CommonHelper.MapPath("~/Plugins/Payments.CityBankApi/"), "createorder.key");
string certificateText = File.ReadAllText(certPath);
string privateKeyText = File.ReadAllText(keyPath);
ICertificateProvider provider = new CertificateFromFileProvider(certificateText, privateKeyText);
//X509Certificate certificate = provider.Certificate;
string accessTokenUrl = "https://sandbox.thecitybank.com:7788/transaction/token";
var requestUrl = new Uri(accessTokenUrl);
var request = (HttpWebRequest)WebRequest.Create(accessTokenUrl);
request.ContentType = "application/json";
request.Method = "POST";
request.ClientCertificates.Add(provider.Certificate);
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = "{\"userName\":\"test\"," +
"\"password\":\"123456Aa\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
catch (Exception ex)
{
throw ex;
}
您的代码似乎是正确的。但请确保您拥有有效证书。如果认证不正确,它将通过异常。检查您的异常消息并尝试检测确切的错误。或 post 此处显示您的错误消息。
此外你应该添加
request.ServerCertificateValidationCallback = (e,r,c,n) => true;
很抱歉,如果重复了。我在互联网上看到几个 post 关于这个话题。但是没有得到任何合适的解决方案。我在 nopcommerce 4.0 工作,它在 .net 核心上是 运行,但目标框架是 4.x.x。我正在开发一个需要与 .crt 和 .key 文件安全连接的支付插件。付款方式提供商发送了一个按预期工作的 php 文件。下面是示例代码
function ProcessRequest($curl_post_data,$service_url,$proxy,$proxyauth)
{
$output = '';
$certfile = '/createorder.crt';
$keyfile = '/createorder.key';
$cert_password = '';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $service_url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_SSLCERT, getcwd() . $certfile );
curl_setopt( $ch, CURLOPT_SSLKEY, getcwd() . $keyfile );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$output = curl_exec($ch);
if (curl_error($ch)) {
echo $error_msg = curl_error($ch);
}
$cblcz = json_decode($output, true );
return $cblcz;
}
$proxy ="";
$proxyauth ="";
$postDatatoken = '{
"password": "123456Aa",
"userName": "test"
}';
$serviceUrltoken ="";
$serviceUrltoken= 'https://sandbox.thecitybank.com:7788/transaction/token';
$cblcz = ProcessRequest($postDatatoken,$serviceUrltoken,$proxy,$proxyauth);
我无法将此 curl 转换为 http post。我在下面试过 link
不工作。对于 openssl,它抛出依赖项未加载。这是我的 C# 代码
try
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
var certPath = Path.Combine(CommonHelper.MapPath("~/Plugins/Payments.CityBankApi/"), "createorder.crt");
var keyPath = Path.Combine(CommonHelper.MapPath("~/Plugins/Payments.CityBankApi/"), "createorder.key");
string certificateText = File.ReadAllText(certPath);
string privateKeyText = File.ReadAllText(keyPath);
ICertificateProvider provider = new CertificateFromFileProvider(certificateText, privateKeyText);
//X509Certificate certificate = provider.Certificate;
string accessTokenUrl = "https://sandbox.thecitybank.com:7788/transaction/token";
var requestUrl = new Uri(accessTokenUrl);
var request = (HttpWebRequest)WebRequest.Create(accessTokenUrl);
request.ContentType = "application/json";
request.Method = "POST";
request.ClientCertificates.Add(provider.Certificate);
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = "{\"userName\":\"test\"," +
"\"password\":\"123456Aa\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
catch (Exception ex)
{
throw ex;
}
您的代码似乎是正确的。但请确保您拥有有效证书。如果认证不正确,它将通过异常。检查您的异常消息并尝试检测确切的错误。或 post 此处显示您的错误消息。
此外你应该添加
request.ServerCertificateValidationCallback = (e,r,c,n) => true;