Socket s=new Socket("176.135.25.29",8091);
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream(),"utf-8"));
PrintWriter out=new PrintWriter(new OutputStreamWriter(s.getOutputStream(),"utf-8"));
out.println("GET /index HTTP/1.1");
out.println("Host: 176.135.25.29:8091");
out.println("Connection: keep-alive\r\n");/**必须要加\r\n表示消息头结束*/
out.println("Upgrade-Insecure-Requests: 1\r\n");
out.println("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64\r\n) AppleWebKit/537.36 (KHTML, like Gecko)\r\n");
out.println("Chrome/71.0.3578.98 Safari/537.36\r\n");
out.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n");
out.println("Accept-Encoding: gzip, deflate\r\n");
out.println("Accept-Language: zh-CN,zh;q=0.9\r\n");
String str=null;
System.out.println("以下是服务器返回的结果:");
while((str = in.readLine())!=null) {
System.out.println(str);
}
s.close();
HTTP协议底层实际还是TCP/IP协议,HTTP请求实际上也是一次TCP/IP请求