Https requests in C #. net,

Source: Internet
Author: User

Https requests in C #. net,
The https connection method was used when connecting to the Huawei NB-IoT platform. Huawei provided certificate files in the crt format, but only provided java examples, C # An error occurred while referencing the certificate. The following is a pitfall:

NB_IoT platform interface debugging and https Authentication

  • First, add
  • 1 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);

    The callback function returns true directly without verifying the certificate.

  • After only one certificate was used to study the https authentication method, it was found that both "Verification" and "device Certificate" were required, so two certificates were imported.
    1 X509Certificate cer = new X509Certificate(@"C:\ca.crt");2 webrequest.ClientCertificates.Add(cer2);

    Later, I found a problem with the connection server address and changed the connection address. The "CryptographicException" error was returned. Find a solution and add the code.

    1 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

    Use the TLS encryption mode. (here, you must upgrade to. net framework4.5 or above to use the Tls encryption mode)

  • I tried to try a certificate in other formats, but none of them were available. When using a certificate in JKS format, an error is reported directly, "the requested object cannot be found". It is suspected that the certificate format is C # not supported, it seems that JKS is a special format used by java tools. after other formats are used, you can generate a certificate, but "failed to create SSL/TLS Secure Channel" is displayed when you return"
  • Change X509Certificate to X509Certificate2. The error 400 is returned, indicating that the certificate is verified but the request format is incorrect.
  • On the morning of the next day (January 1, April 12), a space "Content-Type" was added to "Content-Type" in the Https header, leading to format errors, I do not know how the space is entered by a pure-handed error. After the space is removed, the request is successful. bytes

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.