Results 1 to 3 of 3
  1. #1
    ssraghavan is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default HTTP URL 301 redirect.

    Hi,

    I am trying to fetch all kinds of URL response, but i face the issue when it url is having 301 redirected response i am unable to fetch the url response . Can someone kindly help me to solve this issue.

    Piece of very basic code to pull out http url response is as follows..

    HttpURLConnection yc =(HttpURLConnection) yahoo.openConnection();
    int code = yc.getResponseCode ( ) ;


    System.out.println("mesg is"+ yc.getResponseMessage( ));



    // System.out.println("ok value is "+yc.HTTP_OK);

    BufferedReader in = new BufferedReader(
    new InputStreamReader(
    yc.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null)
    {

    System.out.println(inputLine);
    out.write(inputLine);

    }

    Thanks in advance,
    Rag.

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    I'm not sure what your question is or what you are trying to do. Are you trying to get a 301 message?

    Also, can you post a compilable program so that we can test it. For example, I presume that "yahoo" is a URL object with the site "http://yahoo.com", but it would be more helpful if you told us what yahoo was.


    I'm not sure if this is related, but page redirects are done automatically, by default. If you want to disable this, you can use the following.

    Java Code:
    URL yahoo = new URL("http://yahoo.com");
    HttpURLConnection yc = (HttpURLConnection) yahoo.openConnection();
    
    // don't automatically redirect
    yc.setInstanceFollowRedirects(false);
    
    int code = yc.getResponseCode();
    
    System.out.println(code);
    
    // if redirected, outputs the redirect location
    // (otherwise, null)
    System.out.println(yc.getHeaderField("Location"));
    Last edited by CodesAway; 12-10-2009 at 02:08 AM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    ssraghavan is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default Thanks I got it,

    I was trying fetch the page response for all kinds of URL's and i got stuckup when the URL is being redirected as 301. Thanks, I found the way to find using
    getHeaderField(5) where it gets redirected to, and i get that url, and finally i get the page response..

    Thanks
    Rag

Similar Threads

  1. Redirect IP to 127.0.0.1?
    By Bill89 in forum Advanced Java
    Replies: 12
    Last Post: 12-13-2009, 12:40 PM
  2. Replies: 7
    Last Post: 11-11-2009, 09:29 PM
  3. Http change to HTTP\SSL
    By ballyv24 in forum Advanced Java
    Replies: 0
    Last Post: 05-14-2008, 12:32 PM
  4. How to redirect the output
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-04-2008, 03:36 PM
  5. how to redirect the browser
    By simon in forum Java Applets
    Replies: 1
    Last Post: 08-02-2007, 06:24 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •