其实ASP中 Response.redirect 重定向就是向客户端的浏览器发送一个特殊的HTTP报头:
HTTP/1.1 302 Object Moved
Location http://redirecturl
浏览器读到此报头,就按Location值的指示载入页面.所以,获取重定向后的URL也就是获取HTTP头的Location值.
下面是实现的代码:(vb.net)
'创建一个HttpWebRequest对象 Dim myHttpWebRequest As HttpWebRequest = HttpWebRequest.Create(sRedirectUrl) '禁止自动响应重定向 myHttpWebRequest.AllowAutoRedirect = False '创建一个HttpWebResponse对象 Dim myHttpWebResponse As HttpWebResponse = myHttpWebRequest.GetResponse() '同步方式获取 '获得重定向地址 MessageBox.Show(myHttpWebResponse.Headers("Location")) '获取HTTP报头的Location值
几行代码就搞定了.HOHO
下面的是EXE文件
在ASP中实现
使用方法:
先注册组件 regsvr32 路径/GetRedirectURL.dll
<% dim obj Set obj = Server.CreateObject("GetRedirectURL.GetUrl") With obj.url = "http://www.popx.net/redirect.asp" response.write .execute End With set obj = nothing %>
属性:Url 要获取重定向地址的Url
方法:Execute 成功获取则返回重定向后的地址,否则返回空字符串
在PHP中实现
<?#code-by-kernel#Sample 地址为 http://www.xxx.cn/xx.asp?id=3&no=2 $host = "www.xxx.cn"; // 主机$script = "/xx.asp?id=3&no=2"; //脚本地址 $fp = fsockopen($host,80,$errno,$errstr,30);$header = "GET {$script} HTTP/1.1\r\n";$header .= "Host: https://www.u6u8.net/blog/\r\n";$header .= "Referer: http://https://www.u6u8.net/blog//\r\n";$header .= "Connection: Close\r\n\r\n"; fputs($fp, $header);while (!feof($fp)){ $content.=fgets($fp, 128); }fclose($fp); $pattern = "/(?<=Location: )(http|ftp).+?(?=\r\n)/i"; //这个规则可以根据需要自己改if(preg_match($pattern,$content,$matches)){ echo $matches[0]; // 重定向的地址}?>
简单的说,就是读取页面内容,用正则表达式找出Url,但是不是万能的,如果对方不是用Location :的方法转向,如用
转向,就不能使用。要更改代码中相应的字符
评论列表: