简体   繁体   English

页面重定向时 HttpWebRequest 和 Restsharp 不返回响应

[英]HttpWebRequest and Restsharp do not return response when the page is redirected

I try to create simple console app, where i send request to url and read received response Code:我尝试创建简单的控制台应用程序,在其中向 url 发送请求并读取收到的响应代码:

HttpWebRequest implementation HttpWebRequest 实现

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

RestSharp implementation RestSharp 实现

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.但是当网站重定向到另一个页面或端口时(通常是用 .net 编写的页面,带有反向代理,例如https://www.eventim.pl/ ),程序会尝试发送请求,但从未得到任何响应。

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 实现

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();在这种情况下,它也会在达到response.EnsureSuccessStatusCode();之前超时。

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:该问题与 .NET 或反向代理无关,并且与相关站点永久阻止 HTTP 请求这一事实有关,除非存在以下 HTTP 标头并具有值:

Accept
Accept-Encoding
Accept-Language

For example, the following modification to your code yields a 200 response almost instantly:例如,对您的代码进行以下修改几乎会立即产生 200 响应:

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.将来,请使用浏览器的网络请求控制台和/或 Fiddler 之类的工具来检查 HTTP 请求有效与无效的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM