httpwebrequest返回C#中的错误,在简单的PHP示例中工作



我有工作的php示例,它可以完成其作业,但在.net中无法做到相同的作用 - 返回

system.net.webexception:请求中止:无法创建SSL/TLS安全频道。在system.net.httpwebrequest.getRequestStream(transportContext& contect)

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:xxxxxx" + 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 Ver sions等的变体,但是似乎没有一个工作,我尝试过许多忽略证书错误或使用TLS SSL Versions等的变体正在工作。

(关于重复建议,我认为我已经尝试了所有这些,没有解决我的问题)

php信任您的服务器端或跳过TLS验证,这就是为什么它可以工作的原因。您如何测试?API是否在您发送请求的同一台计算机上?检查一下,也许会有所帮助,因为错误是非常不寻常的。https://support.microsoft.com/en-gb/help/980817/fix-a-net-framework-3-5-1--1--1- as application-becomes-becomes-unresponsive-when-a

我不知道为什么,但是有时会指定正确的contentlength可能会导致此问题。只需删除req.contentlength = buffer.length;解决了问题。

相关内容

  • 没有找到相关文章

最新更新