C # some pitfalls of POST Https requests,

Source: Internet
Author: User

C # some pitfalls of POST Https requests,
Preface:

This problem has occurred since the last time the website was connected with the partner. At that time, when C # was used for POST submission, there was always a problem, after searching for a long time, I found that the target site is TLS 1.2.

Body:

However, the. NET FrameWork 4.0 environment cannot be found... System. Net. SecurityProtocolType does not have this value...

Therefore, problems may occur during POST submission, and some websites will not have this problem because they are 1.0.

So, it seems like this is a pitfall. Fortunately, even if there is no ready-made version, we can use code to implement 1.2.

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;  //SecurityProtocolType.Tls1.2;

Of course, if it is an environment later than 4.0, you can view the enumerated values.

namespace System.Net{    using System;        [Flags]    public enum SecurityProtocolType    {        Ssl3 = 0x30,        Tls = 0xc0,        Tls11 = 0x300,        Tls12 = 0xc00    }}

, Here, all that should be said, and finally the C # https POST code is attached.

Class ProgramTest {static void Main (string [] args) {string url = "https://www.test.com"; string result = PostUrl (url, "key = 123 "); // key = 4da4193e-384b-44d8-8a7f-2dd8b076d784 Console. writeLine (result); Console. writeLine ("OVER"); Console. readLine ();} private static string PostUrl (string url, string postData) {HttpWebRequest request = null; if (url. startsWith ("https", StringComparison. ordinalIgnoreCase) {request = WebRequest. create (url) as HttpWebRequest; ServicePointManager. serverCertificateValidationCallback = new RemoteCertificateValidationCallback (CheckValidationResult); request. protocolVersion = HttpVersion. version11;
// The protocol type is set here. ServicePointManager. securityProtocol = (SecurityProtocolType) 3072; // SecurityProtocolType. tls1.2; request. keepAlive = false; ServicePointManager. checkCertificateRevocationList = true; ServicePointManager. defaultConnectionLimit = 100; ServicePointManager. expect100Continue = false;} else {request = (HttpWebRequest) WebRequest. create (url);} request. method = "POST"; // use get to send a data request. contentType = null; request. referer = null; request. allowAutoRedirect = true; request. userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2 ;. net clr 1.1.4322 ;. net clr 2.0.50727) "; request. accept = "*/*"; byte [] data = Encoding. UTF8.GetBytes (postData); Stream newStream = request. getRequestStream (); newStream. write (data, 0, data. length); newStream. close (); // obtain the webpage response result HttpWebResponse response = (HttpWebResponse) request. getResponse (); Stream stream = response. getResponseStream (); // client. headers. add ("Content-Type", "application/x-www-form-urlencoded"); string result = string. empty; using (StreamReader sr = new StreamReader (stream) {result = sr. readToEnd ();} return result;} private static bool CheckValidationResult (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) {return true; // always accept }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.