Home > Net >  Using HttpWebRequest HTTPS post request
Using HttpWebRequest HTTPS post request

Time:10-03

Existing an HTTPS post request, need client certificate authentication, through the CURL command has post success, want to have the request into a c # httpwebrequest, there has been no success, consult everybody a great god should be how to do?
The CURL command is as follows:
Curl -x POST - F data_src=https://bbs.csdn.net/topics/FOX -f CMD=sendResultDetails -f cmd_data=file address https://XXX -- cacert c:/*. Chain. Pem - key c:/* privatekey. Key - cert c:/*. Cert. Pem

Part of the c # code
 

ServicePointManager. Expect100Continue=true;
ServicePointManager. DefaultConnectionLimit=9999;
ServicePointManager. SecurityProtocol=SecurityProtocolType. Ssl3 | SecurityProtocolType. Tls12 | SecurityProtocolType. Tls11 | SecurityProtocolType. Tls.
//set the validation callback (always)
ServicePointManager. ServerCertificateValidationCallback=new RemoteCertificateValidationCallback (CheckValidationResult);

HttpWebRequest myRequest=(HttpWebRequest) WebRequest. Create (url);//the request of the complete address (IP: port/+ url)

//X509 certificate
X509Certificate2 cert=new X509Certificate2 (CerPath, private_key, X509KeyStorageFlags DefaultKeySet | X509KeyStorageFlags. MachineKeySet | X509KeyStorageFlags. PersistKeySet);
//X509Certificate2 cacert=new X509Certificate2 (CACerPath);
MyRequest. ImpersonationLevel=System. Security. Principal. TokenImpersonationLevel. Impersonation;


MyRequest. Method="POST";
MyRequest. ContentType="application/json; Utf-8 charset=\ '\ "";
MyRequest. Accept="application/json";
//myRequest. UseDefaultCredentials=true;
MyRequest. KeepAlive=true;

MyRequest. ContentLength=data. Length;
MyRequest. ClientCertificates. Add (cert);//add the certificate into the HTTP request
//myRequest. ClientCertificates. Add (cacert);

The Stream newStream=myRequest. GetRequestStream ();
//Send the data.
NewStream. Write (data, 0, the data Length);
NewStream. Close ();

//Get the response
Var response=(HttpWebResponse) myRequest. The method GetResponse ();
Using (var reader=new StreamReader (response. GetResponseStream (), Encoding, GetEncoding (" utf-8 ")))
{
The string result=reader. ReadToEnd ();
Reader. The Close ();
The response. The Close ();
return result;
}

CodePudding user response:

How much is returned a status code,

https://blog.csdn.net/qiaohuyue/article/details/96124416

You can refer to, HttpClient,

CodePudding user response:

The curl -x POST - F data_src=https://bbs.csdn.net/topics/FOX - CMD=sendResultDetails F...

What are you doing in the curl command of multipart Posting,
But I did not see you do multipart/c # code form - the data,
  •  Tags:  
  • C#
  • Related