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

问题描述

我试图提取网页的内容A.使用groovy我试过以下内容:

I am trying to extract the content of a webpage A. Using groovy I've tried the following

......
String urlStr = "url-of-webpage-A"
String pageText = urlStr.toURL().text
//println pageText
.....

只要不重定向到其他网页,上述代码就会检索网页A的文字B.如果A重定向到B,则在pageText变量中检索webPage B的页面内容。有没有一种方法来编写和检查webPage A是否重定向到其他网页(在groovy或java中)?

The above code retrieves the text of webPage A as long as it doesn't redirect to an other webpage B. If A redirects to B, the page content of webPage B is retrieved in the pageText variable. Is there a way to code and check if webPage A is redirecting to an other webpage (in groovy or java)?

PS:上面的代码段不是一部分的服务器端逻辑。我在桌面应用程序的范围内在客户端执行它。

PS: The above piece of code is not a part of server side logic. I am executing it on the client side within the scope of a desktop appilcation.

推荐答案

在groovy中,您可以执行在做:

In groovy, you could do what Joachim suggests by doing:

String location = "url-of-webpage-A"
boolean wasRedirected = false
String pageContent = null

while( location ) {
  new URL( location ).openConnection().with { con ->
    // We'll do redirects ourselves
    con.instanceFollowRedirects = false

    // Get the response code, and the location to jump to (in case of a redirect)
    location = con.getHeaderField( "Location" )
    if( !wasRedirected && location ) {
      wasRedirected = true
    }

    // Read the HTML and close the inputstream
    pageContent = con.inputStream.withReader { it.text }
  }
}

println "wasRedirected:$wasRedirected contentLength:${pageContent.length()}"

如果您不想被重定向,并且希望第一页的内容,你只需要做:

If you don't want to be redirected, and want the contents of the first page, you simply need to do:

String location = "url-of-webpage-A"
String pageContent = new URL( location ).openConnection().with { con ->
  // We'll do redirects ourselves
  con.instanceFollowRedirects = false

  // Get the location to jump to (in case of a redirect)
  location = con.getHeaderField( "Location" )

  // Read the HTML and close the inputstream
  con.inputStream.withReader { it.text }
}

if( location ) {
  println "Page wanted to redirect to $location"
}
println "Content was:"
println pageContent

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

08-26 21:20
查看更多