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.");
}
}
责任编辑:小草