S
S
spamprom2014-06-18 23:21:28
PHP
spamprom, 2014-06-18 23:21:28

How to convert code from PHP to C# (SSL request)?

I have a php code that accesses the service via https

$ch = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->server_url);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT, "User-Agent=Mozilla/5.0 Firefox/1.0.7");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSLCERT, $this->pem_file);   //path_file.pem
curl_setopt($curl, CURLOPT_SSLKEY, $this->key_file);    //path_file.key
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($curl, CURLOPT_CAINFO, $this->chain_file);  //path_file.crt
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$result_curl = curl_exec($ch);
curl_close($ch);
echo $result_curl;

I need to do the same in asp.net (C#) . My attempts were unsuccessful. Here is the code I am using
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
HttpWebRequest rq = (HttpWebRequest)WebRequest.Create(server_url);
rq.Method = "POST";
rq.KeepAlive = false;
rq.UserAgent = "User-Agent=Mozilla/5.0 Firefox/1.0.7";
rq.Proxy = null;
rq.ContentType = "application/www-form-urlencoded";
rq.ProtocolVersion = HttpVersion.Version10;

rq.ClientCertificates.Add(X509Certificate.CreateFromCertFile(chain_file));
rq.ClientCertificates.Add(new X509Certificate2(pem_file));
//rq.ClientCertificates.Add(X509Certificate.CreateFromCertFile(key_file));   ????
string dataToSend = String.Format("my data");
byte[] byteArray = Encoding.UTF8.GetBytes(dataToSend);
rq.ContentLength = byteArray.Length;
Stream dataStream = rq.GetRequestStream();

An error occurs on the last line:
"The request was aborted: Failed to create a secure SSL / TLS channel"
Tell me how to make a request? I can't understand where in C# should I add key_file.key?
P.S. There are many questions on the Internet on the same subject, but they are unanswered or the answer is not working.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Soldatov, 2014-06-19
@n90cd9a

Maybe this will solve the problem: curl.haxx.se/mail/archive-2008-07/0038.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question