模拟浏览器,发送http的消息给tomcat服务器,并获取服务器(Tomcat)反馈的信息
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; public class MyBrowser { public static void main(String[] args) throws UnknownHostException, IOException { /* * 模拟浏览器,发送http的消息给tomcat服务器,并获取服务器反馈的信息 */ Socket s = new Socket("192.168.17.1",8080); //获取输出流,给服务器发送数据 PrintWriter out = new PrintWriter(s.getOutputStream(),true); out.println("GET /myapp/font.html HTTP/1.1"); out.println("Accept: */*"); out.println("Host: 192.168.***.***:8080");//Tomcat的默认端口号是8080 out.println("Connection: close"); out.println(); InputStream in = s.getInputStream(); byte[] buf = new byte[1024]; int len = in.read(buf); String str = new String(buf,0,len); System.out.println(str); s.close(); } }
获取到的http的应答头消息:
HTTP/1.1 200 OK //应答行 http协议版本,应答状态码,应答描述信息
应答属性信息:
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"177-1475663214882"
Last-Modified: Wed, 05 Oct 2016 10:26:54 GMT
Content-Type: text/html
Content-Length: 177
Date: Wed, 05 Oct 2016 10:27:09 GMT
Connection: close
空行
应答体。
<!DOCTYPE html>
<html>
<head>
<meta charset="GBK">
<title>Insert title here</title>
</head>
<body>
<font size="8" color = "red">这是GBK编码的文件</font>
</body>
</html>
时间: 2024-10-11 07:49:43