C# 中的 HttpWebRequest returns 错误,在简单的 php 示例中有效
HttpWebRequest returns error in c#, works in simple php example
我有工作的 php 示例,它完成了它的工作,但不能在 .NET 中做同样的事情 - 返回
System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel. at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream() at
php代码:
$curl = curl_init();
$post_fields = "xxxxxx&msg_type=aaa";
$submit_url = "https://ecommerce.........";
Curl_setopt($curl, CURLOPT_SSLVERSION, 1); //0
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, '0');
curl_setopt($curl, CURLOPT_SSLCERT, getcwd().'/sert.pem');
curl_setopt($curl, CURLOPT_SSLKEYPASSWD, 'XXXXXXXX');
curl_setopt($curl, CURLOPT_URL, $submit_url);
$result = curl_exec($curl);
$info = curl_getinfo($curl);
.NET代码
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string addr1 = "https://ecommerce......";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(addr1);
req.Method = "POST"; // Post method
System.Net.ServicePointManager.ServerCertificateValidationCallback =
delegate
{
return true; // **** Always accept
};
req.KeepAlive = false;
req.ProtocolVersion = HttpVersion.Version10;
//Certificate with private key
string certName = "sss.xxx.ee_5300954_merchant_wp";
string certpwd = "xxxxxxxx";
X509Certificate2 cert = new X509Certificate2(@"c:\xxx\xxx\" + certName + ".pem", certpwd);
req.ServerCertificateValidationCallback =
delegate
{
return true; // **** Always accept
};
req.ClientCertificates.Add(cert);
//req.PreAuthenticate = false;
string langStr = "";
String XML = "description=xxxx&msg_type=SMS";//reader.ReadToEnd();
byte[] buffer = Encoding.ASCII.GetBytes(XML);
req.ContentLength = buffer.Length;
// Wrap the request stream with a text-based writer
Stream writer = req.GetRequestStream();
writer.Write(buffer, 0, buffer.Length);
writer.Close();
WebResponse rsp = req.GetResponse();
String post_response = "";
using (StreamReader responseStream = new StreamReader(rsp.GetResponseStream()))
{
}
我已经尝试了很多忽略证书错误的变体,或者使用 tls ssl 版本等,但是 none 似乎在工作我已经尝试了很多忽略证书错误的变体,或者使用 tls ssl 版本等sions 等,但 none 似乎有效。
(关于复制建议,我想我已经试过了,none 解决了我的问题)
PHP 要么信任您的服务器端,要么跳过 TLS 验证,这就是它起作用的原因。你如何测试它? API 是否在您发送请求的同一台机器上?
看看这个,也许它会有所帮助,因为错误很不寻常。 https://support.microsoft.com/en-gb/help/980817/fix-a-net-framework-3-5-based-application-becomes-unresponsive-when-a
我不知道为什么,但有时指定正确的 ContentLength 可能会导致此问题。
只是删除 req.ContentLength = buffer.Length;
解决了问题。
我有工作的 php 示例,它完成了它的工作,但不能在 .NET 中做同样的事情 - 返回
System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel. at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream() at
php代码:
$curl = curl_init();
$post_fields = "xxxxxx&msg_type=aaa";
$submit_url = "https://ecommerce.........";
Curl_setopt($curl, CURLOPT_SSLVERSION, 1); //0
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, '0');
curl_setopt($curl, CURLOPT_SSLCERT, getcwd().'/sert.pem');
curl_setopt($curl, CURLOPT_SSLKEYPASSWD, 'XXXXXXXX');
curl_setopt($curl, CURLOPT_URL, $submit_url);
$result = curl_exec($curl);
$info = curl_getinfo($curl);
.NET代码
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string addr1 = "https://ecommerce......";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(addr1);
req.Method = "POST"; // Post method
System.Net.ServicePointManager.ServerCertificateValidationCallback =
delegate
{
return true; // **** Always accept
};
req.KeepAlive = false;
req.ProtocolVersion = HttpVersion.Version10;
//Certificate with private key
string certName = "sss.xxx.ee_5300954_merchant_wp";
string certpwd = "xxxxxxxx";
X509Certificate2 cert = new X509Certificate2(@"c:\xxx\xxx\" + certName + ".pem", certpwd);
req.ServerCertificateValidationCallback =
delegate
{
return true; // **** Always accept
};
req.ClientCertificates.Add(cert);
//req.PreAuthenticate = false;
string langStr = "";
String XML = "description=xxxx&msg_type=SMS";//reader.ReadToEnd();
byte[] buffer = Encoding.ASCII.GetBytes(XML);
req.ContentLength = buffer.Length;
// Wrap the request stream with a text-based writer
Stream writer = req.GetRequestStream();
writer.Write(buffer, 0, buffer.Length);
writer.Close();
WebResponse rsp = req.GetResponse();
String post_response = "";
using (StreamReader responseStream = new StreamReader(rsp.GetResponseStream()))
{
}
我已经尝试了很多忽略证书错误的变体,或者使用 tls ssl 版本等,但是 none 似乎在工作我已经尝试了很多忽略证书错误的变体,或者使用 tls ssl 版本等sions 等,但 none 似乎有效。
(关于复制建议,我想我已经试过了,none 解决了我的问题)
PHP 要么信任您的服务器端,要么跳过 TLS 验证,这就是它起作用的原因。你如何测试它? API 是否在您发送请求的同一台机器上? 看看这个,也许它会有所帮助,因为错误很不寻常。 https://support.microsoft.com/en-gb/help/980817/fix-a-net-framework-3-5-based-application-becomes-unresponsive-when-a
我不知道为什么,但有时指定正确的 ContentLength 可能会导致此问题。 只是删除 req.ContentLength = buffer.Length; 解决了问题。