我如何确定URL是否重定向? [英] How can I determine if a URL redirects?

查看:432
本文介绍了我如何确定URL是否重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有网址(例如 http://www.foo.com/ alink.pl?page=2 ),我想确定我是否被重定向到另一个链接。我也想知道最终的网址(例如 http://www.foo.com/other_link。 PL )。最后,我想在Perl和Groovy中做到这一点。

 使用LWP :: UserAgent; 
my $ ua = LWP :: UserAgent-> new;

my $ request = HTTP :: Request-> new(GET =>'http://google.com/');
my $ response = $ ua->请求($ request);
if($ response-> is_success and $ response-> previous){
print $ request-> url,'redirected to',$ response-> request-> uri, \\\
;
}


If I have a URL (eg. http://www.foo.com/alink.pl?page=2), I want to determine if I am being redirected to another link. I'd also like to know the final URL (eg. http://www.foo.com/other_link.pl). Finally, I want to be able to do this in Perl and Groovy.

解决方案

In Perl:

use LWP::UserAgent;
my $ua = LWP::UserAgent->new;

my $request  = HTTP::Request->new( GET => 'http://google.com/' );
my $response = $ua->request($request);
if ( $response->is_success and $response->previous ) {
    print $request->url, ' redirected to ', $response->request->uri, "\n";
}

这篇关于我如何确定URL是否重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆