How to get redirected url as string after request

冷暖自知 提交于 2019-12-25 02:15:34

问题


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.



来源:https://stackoverflow.com/questions/8317204/how-to-get-redirected-url-as-string-after-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!