How to get redirected url as string after request

2019-08-16 13:32发布

问题:

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!)

回答1:

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.



回答2:

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


回答3:

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.