Webapps初步_认识HTTP例子程序读取

package servlet_01;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket; public class Test { /**检测HTTP协议是什么一个东西
* @param args
*/
public static void main(String[] args) throws Exception {
//连到tomcat Server
Socket socket = new Socket("127.0.0.1", 8080);
//拿到输出流
PrintWriter pw = new PrintWriter(new OutputStreamWriter(
socket.getOutputStream()));
pw.println("GET /aaa HTTP/1.1"); //GET+URL地址+HTTP/1.1,自己去看浏览器
pw.println("Host:localhost");//Host+域名
pw.println("Content-type:text/html");
pw.println(); //请求结束
pw.flush();
//输入流读取
BufferedReader br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String str = "";
while ((str = br.readLine()) != null) {
System.out.println(str); }
br.close();
pw.close();
socket.close();
} }

读取tomcat的页面的html内容,了解HTTP协议。

上一篇:从零开始实现ASP.NET Core MVC的插件式开发(四) - 插件安装


下一篇:Set集合、List集合