简易的Java版HttpClient

  1. /**
  2.  * This is a test program for HttpClient.  @author Hu Dennis created at 2008-11-12
  3.  */
  4. import java.net.Socket;
  5. import java.io.BufferedReader;
  6. import java.io.DataInputStream;
  7. import java.io.DataOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. public class HttpClient {
  11.     public static void main(String[] args) {
  12.         String dstHost = "10.56.135.64";
  13.         int    dstPort = 80;
  14.         
  15.         Socket clientSocket = null;
  16.         
  17.         DataOutputStream outBound = null;
  18.         DataInputStream  inBound  = null;
  19.         
  20.         try {
  21.             
  22.             clientSocket = new Socket(dstHost, dstPort);
  23.             clientSocket.setSoTimeout(30000);           
  24.             
  25.             outBound = new DataOutputStream(clientSocket.getOutputStream());
  26.             inBound  = new DataInputStream(clientSocket.getInputStream());
  27.             
  28.             StringBuilder request = new StringBuilder(); 
  29.             
  30.             request.append("GET /test.html HTTP/1.1/n");
  31.             request.append("HOST:10.56.135.64/n/r/n");
  32.             
  33.             outBound.writeBytes(request.toString());
  34.             
  35.             
  36.             BufferedReader bfReader = new BufferedReader(new InputStreamReader(inBound)); 
  37.             
  38.             String responseLine = null;
  39.             
  40.             while ((responseLine = bfReader.readLine()) != null){   
  41.                 
  42.                 System.out.println(responseLine);   
  43.                 if ( responseLine.indexOf("</HTML>") != -1 || responseLine.indexOf("</html>") != -1)   
  44.                     break;   
  45.             }   
  46.             
  47.             outBound.close();
  48.             inBound.close();
  49.             clientSocket.close();
  50.             
  51.         }
  52.         catch (IOException e) {
  53.             System.out.println(e);
  54.         }
  55.         
  56.         System.out.println("This is the end of the program.");
  57.     }
  58. }

你可能感兴趣的:(java,String,null)