Imran Ahmed
Imran Ahmed

Reputation: 386

How to get redirected url as string after request

For example on any tinyurl/ajdeijad link (this one is fake), the think redirects to another url

Here is my code:

    Dim request1 As HttpWebRequest = DirectCast(HttpWebRequest.Create(urlvimeohd), HttpWebRequest)
            request1.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1"
            request1.MaximumAutomaticRedirections = 1
            request1.AllowAutoRedirect = True

How do you retrieve the url of the response (it redirects!)

Upvotes: 0

Views: 6176

Answers (3)

Saqib
Saqib

Reputation: 2283

Try This,

Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("http://tinyurl/ajdeijad"), HttpWebRequest)
Dim response As HttpWebResponse
Dim resUri As String
response = req.GetResponse
resUri = response.ResponseUri.AbsoluteUri
MsgBox(resUri)

This will return the redirected URL.

Upvotes: 0

Imran Ahmed
Imran Ahmed

Reputation: 386

Simple - just get the reponseuri of the response!

http://msdn.microsoft.com/en-us/library/system.net.webresponse.responseuri.aspx

dim myresponse as request1.getresponse()
dim x as string = myresponse.ResponseURI

Upvotes: 1

David Schwartz
David Schwartz

Reputation: 2026

The only way I know of to find which URL it redirects to is by making the request and reading the response.

request1.GetResponse().Headers("Location")

FYI: You should check out Fiddler. It's a free app that will allow you to visually inspect the requests and responses made by your browser. You can copy paste that link you have into your browser and see what the server says back. Then you'll know which Header to check for the info you want.

Hope that helps.

Upvotes: 0

Related Questions