Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-07-2008, 09:59 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Reading Web Pages with Streams
This Java tip shows how to read a web server response using Streams in Java.

Code:
import java.io.BufferedReader; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class GetWebPage { public static void main(String args[]) throws IOException, UnknownHostException { String resource, host, file; int slashPos; resource = "http://www.java-tips.org".substring(7); // skip HTTP:// slashPos = resource.indexOf('/'); if (slashPos < 0) { resource = resource + "/"; slashPos = resource.indexOf('/'); } file = resource.substring(slashPos); // isolate host and file parts host = resource.substring(0, slashPos); System.out.println("Host to contact: '" + host + "'"); System.out.println("File to fetch : '" + file + "'"); MyHTTPConnection webConnection = new MyHTTPConnection(host); if (webConnection != null) { BufferedReader in = webConnection.get(file); String line; while ((line = in.readLine()) != null) { // read until EOF System.out.println(line); } } System.out.println("\nDone."); } static class MyHTTPConnection { public final static int HTTP_PORT = 80; InetAddress wwwHost; DataInputStream dataInputStream; PrintStream outputStream; public MyHTTPConnection(String host) throws UnknownHostException { wwwHost = InetAddress.getByName(host); System.out.println("WWW host = " + wwwHost); } public BufferedReader get(String file) throws IOException { Socket httpPipe; InputStream in; OutputStream out; BufferedReader bufReader; PrintWriter printWinter; httpPipe = new Socket(wwwHost, HTTP_PORT); if (httpPipe == null) { return null; } // get raw streams in = httpPipe.getInputStream(); out = httpPipe.getOutputStream(); // turn into useful ones bufReader = new BufferedReader(new InputStreamReader(in)); printWinter = new PrintWriter(new OutputStreamWriter(out), true); if (in == null || out == null || bufReader == null || printWinter == null) { System.out.println("Failed to open streams to socket."); return null; } // send GET request System.out.println("GET " + file + " HTTP/1.0\n"); printWinter.println("GET " + file + " HTTP/1.0\n"); // read response until blank separator line String response; while ((response = bufReader.readLine()).length() > 0) { System.out.println(response); } return bufReader; } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading Web Pages with Nonblocking Channels Java Tip java.net 0 04-07-2008 10:01 PM
Reading Web Pages with Socket Channels Java Tip java.net 0 04-07-2008 10:00 PM
Character Streams JavaForums Java Blogs 0 12-26-2007 04:01 PM
Data Streams JavaForums Java Blogs 0 12-26-2007 04:01 PM
Using Filtered Streams Java Tip Java Tips 0 12-12-2007 12:30 PM


All times are GMT +3. The time now is 07:08 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org