C# console application not getting intermediate redirects (http 302)

499 views Asked by At

I am trying to get some data downloaded from a URL using HttpWebRequest from a C# console applicaiton. In browser and Postman, it works fine but not from the applicaiton - it does not return the expected data. Using Fiddler, I inspected the request and I figured out that the request initially sent is actually gets redirected thrice (I see three http 302 in Fiddler before the final Http 200 response) and eventually returns the data. However, from my C# console application I get only the final response - the HttpWebResponse status always gives"OK" (200).

I noticed in fiddler that the http 302 returns few cookies and the subsequent request sends the cookies in its header. This is handled correctly in browser/postman but I am not able to do this in my application. Any help will be highly appreciated.

 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); 

Fiddler screenshot

1

There are 1 answers

0
Grim Maple On BEST ANSWER

I believe that you are looking for AllowAutoRedirect property of HttpWebRequest class. Setting it to false should do the thing:

HttpWebRequest request = new HttpWebRequest(someUri);  
request.AllowAutoRedirect = false;  

You can read more info about HttpWebRequest here.