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 8088553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:07:11+00:00 2026-06-05T19:07:11+00:00

My company doesn’t use Apache, so we’ve spun up our own socket code. In

  • 0

My company doesn’t use Apache, so we’ve spun up our own socket code. In the following, HTMLDoc is my version of a server response. I’m trying to grab the URL that a browser would redirect to if the original URL gave a 301 response code:

    static public void main(String args[]) throws Exception
{

    String spec = "http://some_url_with_301_redirect.com";
    URL my_url = new URL(spec);

    HTMLDoc doc = getDocFromURL(my_url);

    // Do stuff with the doc.
}

public static HTMLDoc getDocFromURL(URL url)
{
    try
    {
        URLConnection u = url.openConnection();

        if ( u instanceof HttpURLConnection )
        {
            HttpURLConnection http_u = (HttpURLConnection)u;

            int response_code = http_u.getResponseCode();

            if (response_code == 300 || response_code == 301 || response_code == 302)
            {
                URL redirected_url = getRedirectedURL(url);

                return getDocFromURL(redirected_url);
            }
        }

        return null;
    }

    catch(Exception e) 
    {
        e.printStackTrace();
        return null;
    }
}

The trouble is, I have no idea what the method getRedirectedURL(url) should look like. Is there a quick call I can do to http_u that I’m not familiar with?

  • 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-06-05T19:07:13+00:00Added an answer on June 5, 2026 at 7:07 pm

    A HTTP 301 response is always accompanied by a Location header. A typical response looks looks like

    HTTP/1.1 301 Moved Permanently
    Location: http://www.example.org/index.asp
    

    You can read this header and get the redirected URL. The sample code to do this is mentioned in the link posted by Jon Lin in the comments above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My company doesn't allow the use of boost (for many stupid reasons, but that's
Our company is making a mobile version of our website. We have several product
My shared hosting company doesn't allow Reflection. How can I use AutoMapper? Do I
Our company uses own (built in here) scripting language for programming, but they would
Our company, being rather small, doesn't have much in the way of policies and
I used to love using IntelliJ but my new company doesn't spend money on
I'm building a rails gallery app for a company that doesn't want to host
Our company does work environment surveys, and these surveys are filled in online. All
My company doesn't allow that crash reports are stored on third party servers so
Consider the following statements We are not talking about a well established company in

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.