Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is HttpClient not able to get the response for this site?

It is running into timeout. But the site seems to work normally in browsers.

string uri = "https://helpx.adobe.com/de/robohelp/using/basics-of-conditional-content.html";
HttpClientHandler handler1 = new();
HttpClient client1 = new HttpClient(handler1);
HttpRequestMessage httpRequestMessage1 = new(HttpMethod.Get, uri);
HttpResponseMessage resp1 = client1.SendAsync(httpRequestMessage1).GetAwaiter().GetResult();
string content = await resp1.Content.ReadAsStringAsync();
Console.WriteLine(content);

I have executed this simple code in Visual Studio 2022 on Windows 10, .NET 8, but it runs into timeout waiting for the response.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The site likely blocks because of empty headers. When added it work.

string uri = "https://helpx.adobe.com/de/robohelp/using/basics-of-conditional-content.html";
HttpClientHandler handler1 = new();
HttpClient client1 = new HttpClient(handler1);
client1.DefaultRequestHeaders.Add("User-Agent", "curl/8.9.1");
client1.DefaultRequestHeaders.Add("Accept", "*/*");
HttpRequestMessage httpRequestMessage1 = new(HttpMethod.Get, uri);
HttpResponseMessage resp1 = client1.SendAsync(httpRequestMessage1).GetAwaiter().GetResult();
string content = await resp1.Content.ReadAsStringAsync();
Console.WriteLine(content);

Btw, the code can be simplified. Also don’t mix async and sync code.

string uri = "https://helpx.adobe.com/de/robohelp/using/basics-of-conditional-content.html";
using (HttpClient client1 = new HttpClient())
{
    client1.DefaultRequestHeaders.Add("User-Agent", "curl/8.9.1");
    client1.DefaultRequestHeaders.Add("Accept", "*/*");
    string content = await client1.GetStringAsync(uri);
    Console.WriteLine(content);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading