Reputation: 750
I am trying to access this link through HttpClient
but every time it says that IsSuccessStatusCode
is false
. In the past I was able to get the content but now it wont work. It gives me 302
response code.
the code that I am trying is:
var handler = new HttpClientHandler()
{
AllowAutoRedirect = false,
UseCookies = true,
PreAuthenticate = true,
UseDefaultCredentials = true
};
var client = new HttpClient(handler);
//client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
var data = await client.GetAsync(Url);
if (!data.IsSuccessStatusCode)
{
;
}
var doc = new HtmlDocument();
var content = await data.Content.ReadAsStringAsync();
Can someone tell me what I am doing wrong here and how can I make it work? So I can get the content. Thanks P.S. I have the permission of the website owners to use the website.
Upvotes: 0
Views: 2274
Reputation: 12858
AllowAutoRedirect = false // change this to true
IsSuccessStatusCode
?Let's start by looking at the HttpResponseMessage Class for the implementation of the IsSuccessStatusCode
property.
public bool IsSuccessStatusCode
{
get { return ((int)statusCode >= 200) && ((int)statusCode <= 299); }
}
As you can see, the 302 status code in will return a
false
.
The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.
The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).
If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
Source: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3
Upvotes: 2