Code: Select all
using var httpClientHandler = new HttpClientHandler() { MaxAutomaticRedirections = 10, AllowAutoRedirect = true};
using var httpClient = new HttpClient(httpClientHandler);
var httpRequest = new HttpRequestMessage(HttpMethod.Get, "https://www.imf.org/en/live");
httpRequest.Headers.TryAddWithoutValidation("authority", "www.imf.org");
httpRequest.Headers.TryAddWithoutValidation("method", "GET");
httpRequest.Headers.TryAddWithoutValidation("scheme", "https");
httpRequest.Headers.TryAddWithoutValidation("accept", "text/html");
httpRequest.Headers.TryAddWithoutValidation("accept-encoding", "gzip");
httpRequest.Headers.TryAddWithoutValidation("accept-language", "en-US,en;q=0.9");
httpRequest.Headers.TryAddWithoutValidation("cache-control", "no-cache");
httpRequest.Headers.TryAddWithoutValidation("pragma", "no-cache");
var response = await httpClient.SendAsync(httpRequest);
response.EnsureSuccessStatusCode();
Code: Select all
{"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.."}
Wenn ich das gleiche httprequest von PowerShell probiere, funktioniert es einwandfrei und gibt einige Inhalte zurück
Code: Select all
Invoke-WebRequest -UseBasicParsing -Uri "https://www.imf.org/en/live" `
-Headers @{
"authority"="www.imf.org"
"method"="GET"
"scheme"="https"
"accept"="text/html"
"accept-encoding"="gzip"
"accept-language"="en-US,en;q=0.9"
"cache-control"="no-cache"
"pragma"="no-cache"
}
StatusCode : 200
StatusDescription : OK
Content :