I have a URL that returns a HTTP 302 redirect, and I would like to get the URL it redirects to.
The problem is that System.Net.WebClient seems to actually follow it, which is bad. HttpWebRequest seems to do the same.
Is there a way to make a simple HTTP Request and get back the target Location without the WebClient following it?
I'm tempted to do raw socket communication as HTTP is simple enough, but the site uses HTTPS and I don't want to do the Handshaking.
At the end, I don't care which class I use, I just don't want it to follow HTTP 302 Redirects :)
Also, for someone who just needs the new location,
HttpResponseMessage
has aRequestMessage
property. Sometimes it can be useful, becauseWebClient
doesn't support changing theAllowAutoRedirect
property once it's been set.It's pretty easy to do
Let's assume you've created an HttpWebRequest called myRequest
Excuse any compile errors I don't have VS right in front of me, but I've used this in the past to check for redirects.
On
HttpWebRequest
you can setAllowAutoRedirect
tofalse
to handle the redirect yourself.The
HttpWebRequest
has a propertyAllowAutoRedirect
which you can set to false (it is always true forWebClient
), and then get theLocation
HTTP header.