How do I use WebRequest to access an SSL encrypted site using HTTPS?
You're doing it the correct way but users may be providing urls to sites that have invalid SSL certs installed. You can ignore those cert problems if you put this line in before you make the actual web request:
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
where AcceptAllCertifications
is defined as
public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
Using HTTPS and httpWebRequest
Simply swapping http with https is fine enough while using HttpWebRequest
. It requires no special handling for https requests.
Access to an SSL encrypted WebResource fails via a ComVisible DLL
I found the solution through Tav's comment. While ServicePointManager.SecurityProtocol contains the protocols TLS | TLS11 | TLS12 for .NET applications, only the protocols SSL3 and TLS are assigned as COM application.
Since Azure AppServices are configured by default so that the minimum TLS version is 1.2, you must either reconfigure the AppService accordingly or explicitly add TLS1.2 in the COM assembly.
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
How to implement SSL with HttpWebRequest?
How to get the SSL certificate into the HttpWebRequest class?
Your problem is not related to SSL at all.
private string acceptEncoding = "Accept-Encoding: gzip, deflate, br";
...
request.Headers.Add(acceptEncoding);
With this code you explicitly tell the server that you'll support various content compression algorithms. Only, this is a lie since you don't deal with compression when reading the response.
\u001f�\b\0\0\0\0\0\0\0
This looks the beginning of a gzip data stream. It starts with hex \x1f\x8b but you interpret this wrongly as UTF-8 which it is not.
The easiest way to fix this part is to remove the Accept-Encoding
from your request or use Accept-Encoding: identity
to signal that you don't accept any compression. This way a well behaving server will send you the body without any compression.
Create a SSL WebRequest with C#
This looks like what you're after
How to use HTTP GET request in C# with SSL? (protocol violation)
But changing "http" to "https" might be a good starting point!
Then, apparently, setting a ServicePointManager.ServerCertificateValidationCallback as shown in 2 of the replies to the above post.
EDIT
Add
string ua = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0";
then
client.Headers.Add(HttpRequestHeader.UserAgent, ua);
Then parse the Location header of the redirected result.
Making a HTTPS request using C# Smart Device Project .net 2.0
Got the answer to my own problem.
I could create a C++ module to handle the HTTPS requests instead, and using C#'s pinvoke method to call the C++ module and returning the results.
Alternatively I could use make a server that handles HTTP requests like a middleman, similar to the C++ module. This server makes the HTTPS requests on behalf of the Smart Device Application to return the results.
TL;DR it is nothing much but just using the middleman approach to solve the problem since there is no function in the Smart Device Application template that could handle HTTPS requests.
Related Topics
Is a Memory Leak Created If a Memorystream in .Net Is Not Closed
Pass Table Valued Parameter Using Ado.Net
Why Can't I Unbox an Int as a Decimal
How to Get All Constants of a Type by Reflection
Alphanumeric Sorting Using Linq
How to Close a Streamwriter Without Closing Its Basestream
Best Way to Access a Control on Another Form in Windows Forms
Using Xmlserializer to Serialize Derived Classes
Dependency Injection in Attributes
Is There a .Net/C# Wrapper for SQLite
Xmlserializer Giving Filenotfoundexception at Constructor
How to Do Pagination in ASP.NET MVC
How to Set Aspnetcore_Environment to Be Considered for Publishing an ASP.NET Core Application
How to Get the Title of the Current Active Window Using C#
How to Convert This Foreach Code to Parallel.Foreach
Omitting All Xsi and Xsd Namespaces When Serializing an Object in .Net