- /**
- * This is a test program for HttpClient. @author Hu Dennis created at 2008-11-12
- */
- 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.");
- }
- }
简易的Java版HttpClient
最新推荐文章于 2023-03-20 16:47:33 发布