JAVA基础辅导(Java版HttpClient)
来源:优易学  2011-11-15 12:10:33   【优易学:中国教育考试门户网】   资料下载   IT书店

 import java.net.Socket;
  import java.io.BufferedReader;
  import java.io.DataInputStream;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  public class HttpClient {
  public static void main(String[] args) {
  String dstHost = "10.56.135.64";
  int dstPort = 80;
  Socket clientSocket = null;
  DataOutputStream outBound = null;
  DataInputStream inBound = null;
  try {

  clientSocket = new Socket(dstHost, dstPort);
  clientSocket.setSoTimeout(30000);
  outBound = new DataOutputStream(clientSocket.getOutputStream());
  inBound = new DataInputStream(clientSocket.getInputStream());
  StringBuilder request = new StringBuilder();
  request.append("GET /test.html HTTP/1.1\n");
  request.append("HOST:10.56.135.64\n\r\n");
  outBound.writeBytes(request.toString());  
  BufferedReader bfReader = new BufferedReader(new InputStreamReader(inBound));
  String responseLine = null;
  while ((responseLine = bfReader.readLine()) != null){
  System.out.println(responseLine);
  if ( responseLine.indexOf("</HTML>") != -1 || responseLine.indexOf("</html>") != -1)
  break;
  }
  outBound.close();
  inBound.close();
  clientSocket.close();
  }
  catch (IOException e) {
  System.out.println(e);
  }
  System.out.println("This is the end of the program.");
  }
  }

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训