Results 1 to 5 of 5
  1. #1
    danilson is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default Getting content from a webpage--help!

    Hi all,
    I am trying to get the hole html code from a webpage but I have a problem.
    Here is part of the code:
    final java.io.InputStream stream = conn.getErrorStream();
    if ( stream != null )
    content = readStream( length, stream );
    else if ( (content = conn.getContent( )) != null &&
    content instanceof java.io.InputStream )
    content = readStream( length, (java.io.InputStream)content );
    conn.disconnect( );
    I also have a getContent method, that returns the content.
    In the main method I get the content: Object content = file.getContent( );
    And I print it : System.out.println(content);
    For some webpages it works but for others it prints something like:
    [B@5f934ad
    Does anyone know why this happens?
    I can provide the whole code (wich I found in the internet) if needed
    Last edited by danilson; 11-23-2010 at 03:20 PM.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    I'm not sure you've given us enough code here.
    I mean, we don;t actually know what half of these variables are...

    Is "file" a URLConnection?
    If so then getContent() returns an Object for a reason, because the content could be (for example) a zip file, or a stream containing the html of a page, or something else entirely.

    Before printing out the content, print out the class of content. I would bet some of the content is not of a class that has a useable toString() method.

  3. #3
    danilson is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    I'm not sure you've given us enough code here.
    I mean, we don;t actually know what half of these variables are...

    Is "file" a URLConnection?
    If so then getContent() returns an Object for a reason, because the content could be (for example) a zip file, or a stream containing the html of a page, or something else entirely.

    Before printing out the content, print out the class of content. I would bet some of the content is not of a class that has a useable toString() method.
    Well, you are right. I just though that someone would have known what could be wrong in only some cases.
    First of all, your bet is correct. When content is what I want the class is: class java.lang.String and when not it is: class [B

    So here is how I make the connection:
    public WebFile(String urlString) throws java.net.MalformedURLException, java.io.IOException {
    // Open a URL connection.
    final java.net.URL url = new java.net.URL(urlString);
    final java.net.URLConnection uconn = url.openConnection();
    if (!(uconn instanceof java.net.HttpURLConnection))
    throw new java.lang.IllegalArgumentException("URL protocol must be HTTP.");
    final java.net.HttpURLConnection conn = (java.net.HttpURLConnection) uconn;

    // Set up a request.
    conn.setInstanceFollowRedirects(true);
    conn.setRequestProperty("User-agent", "spider");

    // Send the request.
    conn.connect();
    Then I get the response and then the content as I described before.

    And in main I do:
    WebFile file = new WebFile( "http://..." );

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    "class [B".
    That's a byte[]...
    Use getContentType() to find out what is being sent. That'll give you the content-type data from the header.

  5. #5
    danilson is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    "class [B".
    That's a byte[]...
    Use getContentType() to find out what is being sent. That'll give you the content-type data from the header.
    Hi, I am sorry I haven't answered for so long but I was away. There was a "bug" in the code. In the websites it didn't work I was getting length to be -1 wich obviously is wrong. But I found a better way to do it.
    Anyway, thanks for your help

Similar Threads

  1. Cannot Get gif to show on webpage
    By ShinTec in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 09-26-2010, 01:36 PM
  2. Navigation inside a webpage...?
    By JavaStudent23 in forum New To Java
    Replies: 1
    Last Post: 03-27-2010, 11:54 AM
  3. Add a Swing UI in a WebPage
    By ivvgangadhar in forum AWT / Swing
    Replies: 9
    Last Post: 06-30-2009, 08:16 AM
  4. Replies: 2
    Last Post: 05-30-2009, 10:42 AM
  5. save a webpage.....?
    By 05bit044 in forum Networking
    Replies: 1
    Last Post: 08-04-2008, 04:49 PM

Posting Permissions

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