Home > Back-end >  HttpWebRequest and Restsharp do not return response when the page is redirected
HttpWebRequest and Restsharp do not return response when the page is redirected

Time:12-08

I try to create simple console app, where i send request to url and read received response Code:

HttpWebRequest implementation

HttpWebRequest init = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)init.GetResponse();
Console.WriteLine(resp.StatusCode);

RestSharp implementation

var client = new RestClient(url);
client.Timeout = -1;
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.StatusCode);

In most cases it work perfectly. But when website redirect to another page or port (very often pages written in .net with reverse proxy eg. https://www.eventim.pl/) the program tries to send the request but never gets any response.

I tried to use:

//HttpWebRequest
init.AllowAutoRedirect = true;

//RestSharp
client.FollowRedirects = true;

However, the situation is the same, the request is sent, but never receives a response, which ends with a timeout.

EDIT I: HttpClient implementation

HttpClient client = new HttpClient();
try
{
  HttpResponseMessage response = await client.GetAsync("https://www.eventim.pl");
  response.EnsureSuccessStatusCode();
  var res = response.StatusCode;
  Console.WriteLine((int)res);
}
catch (HttpRequestException e)
{
  Console.WriteLine("\nException Caught!");
  Console.WriteLine("Message :{0} ", e.Message);
}

In this case, it also gets a timeout before reaching response.EnsureSuccessStatusCode();

CodePudding user response:

The issue has nothing to do with .NET or reverse proxies, and everything to do with the fact that the site in question blocks the HTTP request forever unless the following HTTP headers are present and have values:

Accept
Accept-Encoding
Accept-Language

For example, the following modification to your code yields a 200 response almost instantly:

var client = new HttpClient();

try
{
    var request = new HttpRequestMessage(HttpMethod.Get, "https://www.eventim.pl");
    request.Headers.Add("Accept", "text/html");
    request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
    request.Headers.Add("Accept-Language", "en");

    var response = await client.SendAsync(request);
    response.EnsureSuccessStatusCode();
    var res = response.StatusCode;
    Console.WriteLine((int)res);
}
catch (Exception e)
{
    Console.WriteLine("\nException Caught!");
    Console.WriteLine("Message: {0}", e.Message);
}

In future, use your browser's network request console and/or a tool like Fiddler to check what's happening for HTTP requests that work versus those that don't.

  • Related