Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6560087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:24:52+00:00 2026-05-25T13:24:52+00:00

I am trying to extract the content of a webpage A. Using groovy I’ve

  • 0

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
.....

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: 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:24:52+00:00Added an answer on May 25, 2026 at 1:24 pm

    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    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to extract the answer text on the following webpage. I opened up
I am trying to extract the content following a hashtag using php. For example:
I am trying to extract the video id from a youtube url using the
hi all I'm trying to extract the META description from a webpage using libxml
I am trying to extract table content from a html file using HTML::TableExtract. My
Possible Duplicate: Extract Table Contents using Perl I am trying to extract table content
I am trying to extract the specific content in html using Jsoup. Below is
I am trying to extract the text content of a html file generated bu
I'm trying to parse a website's source to extract text inside using lxml library.
I am trying to extract from a webpage which has the following markup <div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.