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