Results 1 to 3 of 3
  1. #1
    mjzammit is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Default GET html contents from a web server

    hi
    i am building a program to get the contents of html on an http website.
    The code is found below:
    //Get details of the url (RequestTrial) I would like to connect to
    //The 'RequestTrial' stands for the url
    url = new URL(RequestTrial);
    String getHost = url.getHost();
    int getPort = url.getPort();
    if (getPort == -1) {
    getPort = 80;
    }
    SrvrSocket = new Socket(getHost, getPort);
    System.out.println("connected to the server requested");

    //Now i send the GET request to the server
    PrintWriter wrServer = new PrintWriter(new OutputStreamWriter(SrvrSocket.getOutputStream()));
    wrServer.println("GET " + RequestTrial + " HTTP/1.0" + "\r\n");
    wrServer.println(""); // followed by newline
    wrServer.flush();

    //Receiving the data from the server
    // Check reply headers
    DataInputStream Din =
    new DataInputStream(SrvrSocket.getInputStream());
    String str = Din.readLine();
    HTMLstring += str;
    System.out.println(str);
    String tempStr = new String(str + "\r\n");
    if (str.length() > 0) {
    while (true) {
    str = Din.readLine();
    tempStr = new String(str + "\r\n");
    //HTMLstring += tempStr;
    System.out.println(tempStr);

    if (str.length() <= 0) {
    break;
    }
    }
    }

    InputStream rdServer = SrvrSocket.getInputStream();
    InputStreamReader isReader = new InputStreamReader(rdServer);
    BufferedReader rd = new BufferedReader(isReader);

    //HTMLstring is where i am holding the HTML given by the server
    String s = null;
    while ((s = rd.readLine()) != null) {

    HTMLstring += s + "\r\n";

    }

    rd.close();

    } catch (UnknownHostException uhe) {
    // Requested Server could not be located
    System.out.println("Server Not Found: " + uhe);

    } catch (IOException ioe) {
    System.out.println("IOException: " + ioe);
    } finally {
    try {
    SrvrSocket.close();
    } catch (Exception e) {
    System.out.println("Exception: " + e);
    }
    }

    But unfortunately i only receive half of the html
    why is that???

    When i use the URL class getcontent I get all the html, but i need to use sockets. Can someone please indicate where my error is please...

  2. #2
    strati1991 is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default

    you can use
    URLConnection con = yourURL.openConnection();
    BufferedReader in = new BufferedReader(
    new InputStreamReader(
    con.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null)
    System.out.println(inputLine);
    in.close();

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Don't try coding to http. Use an HTTP connection, which is what you get behind the scenes with the advice strat1991 gave.

Similar Threads

  1. How can I include a html file in html textarea?
    By surya_dks in forum New To Java
    Replies: 2
    Last Post: 10-04-2008, 08:20 AM
  2. Viewing contents of zip file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-03-2008, 06:16 PM
  3. Concatenation file contents
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 02-07-2008, 02:29 PM
  4. Reading web contents
    By javaplus in forum Networking
    Replies: 2
    Last Post: 11-29-2007, 11:28 AM

Posting Permissions

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