Java 带userAgent的JSoup阻止重定向

Java 带userAgent的JSoup阻止重定向,java,jsoup,Java,Jsoup,我使用JSoup作为我的网络爬虫 Connection con = Jsoup.connect("http://t.co/uySIPVNfgP"); Document doc = con.get(); String u = doc.baseUri(); 上面给出了重定向的url作为基本uri 但用户代理设置如下: con.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Fir

我使用JSoup作为我的网络爬虫

Connection con = Jsoup.connect("http://t.co/uySIPVNfgP");
Document doc = con.get();
String u = doc.baseUri();
上面给出了重定向的url作为基本uri

但用户代理设置如下:

con.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
Document doc = con.get();
Elements redirEle = doc.head().select("meta[http-equiv=refresh]");
String content = redirEle.get(0).attr("content");
Pattern pattern = Pattern.compile("^.*URL=(.+)$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
if (matcher.matches() && matcher.groupCount() > 0) {
     String redirectUrl = matcher.group(1);
     if(redirectUrl.startsWith("'")){
         /*removes single quotes of urls within single quotes*/
         redirectUrl = redirectUrl.replaceAll("(^')|('$)","");
     }
     if(redirectUrl.startsWith("/")){
         String[] splitedUrl = url.split("/");
         redirectUrl = splitedUrl[0]+"//"+splitedUrl[2]+redirectUrl;
     }
     return redirectUrl;
}
与上述不遵循重定向

正如我所知,没有用户代理,一些网站不允许对其内容进行爬网

如何解决此问题?

请参阅。在那里可以找到以下重定向(布尔)方法:

播放参数,直到获得所需的结果

请参阅。在那里可以找到以下重定向(布尔)方法:

播放参数,直到获得所需的结果

设置用户代理时,服务器端重定向似乎没有响应。但它在html页面中以元重定向的形式发送重定向

使用jsoup,我能够捕获重定向的url,如下所示:

con.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
Document doc = con.get();
Elements redirEle = doc.head().select("meta[http-equiv=refresh]");
String content = redirEle.get(0).attr("content");
Pattern pattern = Pattern.compile("^.*URL=(.+)$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
if (matcher.matches() && matcher.groupCount() > 0) {
     String redirectUrl = matcher.group(1);
     if(redirectUrl.startsWith("'")){
         /*removes single quotes of urls within single quotes*/
         redirectUrl = redirectUrl.replaceAll("(^')|('$)","");
     }
     if(redirectUrl.startsWith("/")){
         String[] splitedUrl = url.split("/");
         redirectUrl = splitedUrl[0]+"//"+splitedUrl[2]+redirectUrl;
     }
     return redirectUrl;
}
当设置用户代理时,似乎不响应服务器端重定向。但它在html页面中以元重定向的形式发送重定向

使用jsoup,我能够捕获重定向的url,如下所示:

con.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
Document doc = con.get();
Elements redirEle = doc.head().select("meta[http-equiv=refresh]");
String content = redirEle.get(0).attr("content");
Pattern pattern = Pattern.compile("^.*URL=(.+)$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
if (matcher.matches() && matcher.groupCount() > 0) {
     String redirectUrl = matcher.group(1);
     if(redirectUrl.startsWith("'")){
         /*removes single quotes of urls within single quotes*/
         redirectUrl = redirectUrl.replaceAll("(^')|('$)","");
     }
     if(redirectUrl.startsWith("/")){
         String[] splitedUrl = url.split("/");
         redirectUrl = splitedUrl[0]+"//"+splitedUrl[2]+redirectUrl;
     }
     return redirectUrl;
}

但是我希望它跟随重定向但是我希望它跟随重定向