Download WWW Page : Socket « Network Protocol « Java






Download WWW Page

    
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;

class Main{
  public String downloadWWWPage(URL pageURL) throws Exception {
    String host, file;
    host = pageURL.getHost();
    file = pageURL.getFile();

    InputStream pageStream = getWWWPageStream(host, file);
    if (pageStream == null) {
      return "";
    }

    DataInputStream in = new DataInputStream(pageStream);
    StringBuffer pageBuffer = new StringBuffer();
    String line;

    while ((line = in.readUTF()) != null) {
      pageBuffer.append(line);
    }
    in.close();
    return pageBuffer.toString();
  }

  public InputStream getWWWPageStream(String host, String file) throws IOException,
      UnknownHostException {
     
    InetAddress webServer = InetAddress.getByName(host);

    Socket httpPipe = new Socket(webServer, 80);
    if (httpPipe == null) {
      System.out.println("Socket to Web server creation failed.");
      return null;
    }

    InputStream inn = httpPipe.getInputStream(); // get raw streams
    OutputStream outt = httpPipe.getOutputStream();

    DataInputStream in = new DataInputStream(inn); // turn into higher-level ones
    PrintStream out = new PrintStream(outt);

    if (inn == null || outt == null) {
      System.out.println("Failed to open streams to socket.");
      return null;
    }
    out.println("GET " + file + " HTTP/1.0\n");

    String response;
    while ((response = in.readUTF()).length() > 0) {
      System.out.println(response);
    }

    return in;
  }
}

   
    
    
    
  








Related examples in the same category

1.Create a socket without a timeout
2.Create a socket with a timeout
3.Demonstrate Sockets.
4.Socket connection and concurrent package
5.XML based message
6.ObjectInputStream and ObjectOutputStream from Socket
7.ServerSocket and Socket for Serializable object
8.String based communication between Socket
9.Get email with Socket
10.Create PrintWriter from BufferedWriter, OutputStreamWriter and Socket
11.Read from server
12.Use Socket to read and write stream
13.Connects to a server at a specified host and port. It reads text from the console and sends it to the server
14.A simple network client that establishes a network connection to a specified port on a specified host, send an optional message across the connection
15.Reading Text from a Socket
16.Writing Text to a Socket
17.Sending a POST Request Using a Socket
18.Get the Date from server
19.Transfer a file via Socket
20.Ping a server
21.Read and write through socket
22.Read float number from a Socket
23.Read Object from Socket
24.deleting messages from a POP3 mailbox based on message size and Subject line
25.A timeout feature on socket connections
26.Write Objects From Socket
27.Write Double Using Sockets
28.Redirects incoming TCP connections to other hosts/ports
29.Socket Fetcher
30.Socket Address Encoder
31.Zip socket
32.This program shows how to interrupt a socket channel.
33.This program shows how to use sockets to send plain text mail messages.
34.Makes a socket connection to the atomic clock in Boulder, Colorado, and prints the time that the server sends.