在C#中调用https://api.codebazan.ir/arz/?type=arz时出现System.Net.Http.HttpRequestException异常(SSL连接无法建立)的解决求助
Hey there! The The SSL connection could not be established error you're hitting almost certainly boils down to TLS protocol version incompatibility. Let’s break this down and fix it:
Why this happens
Windows 8.1’s default HttpClient setup doesn’t enable modern TLS versions (like TLS 1.2 or 1.3) out of the box. The API at https://api.codebazan.ir/arz/?type=arz likely requires one of these newer, more secure protocols to connect, while the other API you tested (https://corona.lmao.ninja/v3/covid-19/countries?sort=cases) still supports older, deprecated TLS versions that Windows 8.1 enables by default.
Fix 1: Force TLS 1.2 in your code
The simplest fix is to explicitly set the security protocol before creating your HttpClient instance. Add this line right at the start of your Main method:
static async System.Threading.Tasks.Task Main(string[] args) { // Enable TLS 1.2 (required by most modern secure APIs) System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; using (HttpClient client = new HttpClient()) { string request = "https://api.codebazan.ir/arz/?type=arz"; HttpResponseMessage apirequest = await client.GetAsync(request); string jsonResponse = await apirequest.Content.ReadAsStringAsync(); } }
If TLS 1.2 doesn’t work, you can try adding TLS 1.3 support (note: Windows 8.1 has limited support for TLS 1.3, so this is a secondary option):
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls13;
Fix 2: Check for untrusted SSL certificates (if Fix 1 fails)
If setting the TLS version doesn’t resolve the issue, the problem might be that the API’s SSL certificate isn’t trusted by your Windows 8.1 system’s root certificate store. Here’s what you can do:
- Open the API URL in a browser on your Windows 8.1 machine. If you see a certificate warning, follow the prompts to manually install and trust the root certificate.
- For testing only: Temporarily skip certificate validation (never do this in production—it exposes you to major security risks):
static async System.Threading.Tasks.Task Main(string[] args) { System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; using (HttpClientHandler handler = new HttpClientHandler()) { // Bypass certificate validation (TESTING ONLY) handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true; using (HttpClient client = new HttpClient(handler)) { string request = "https://api.codebazan.ir/arz/?type=arz"; HttpResponseMessage apirequest = await client.GetAsync(request); string jsonResponse = await apirequest.Content.ReadAsStringAsync(); } } }
Why the other API works
The corona.lmao.ninja API’s server is configured to support older TLS versions (like TLS 1.0) that Windows 8.1 enables by default, which is why you don’t get an error there. Modern APIs are phasing out these older protocols for security reasons, though.
内容的提问来源于stack exchange,提问作者reza setareh

