package test.com.ict.testcountry; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ConnectException; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketTimeoutException; /** * 用socket来收发http协议报文 * @author luoguansong lgsstart89@163.com */ public class SocketHttp { public static void main(String[] args) { Thread threadReceive=new Thread(new TestReceiveHttp()); threadReceive.start(); // Thread threadSend=new Thread(new TestSendHttp()); // threadSend.start(); } } class TestSendHttp implements Runnable{ @Override public void run() { try { String path = "/"; String host = "localhost"; int port = 50000; Socket socket = new Socket(); InetSocketAddress address=new InetSocketAddress(host, port); socket.connect(address,3000); OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream(),"utf-8"); osw.write("GET " + path + " HTTP/1.1 "); osw.write("Host: " + host + " "); //http协议必须在报文头后面再加一个换行,通知服务器发送完成,不然服务器会一直等待 osw.write(" "); osw.flush(); socket.shutdownOutput(); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(socket.getInputStream(), "utf-8")); String line = null; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } osw.close(); bufferedReader.close(); socket.close(); }catch (ConnectException e) { System.out.println("连接失败"); }catch (SocketTimeoutException e) { System.out.println("连接超时"); }catch (Exception e) { System.out.println(e.getMessage()); } } } class TestReceiveHttp implements Runnable{ @Override public void run() { ServerSocket server; Socket socket; try{ server=new ServerSocket(50000); System.out.println("正在等待50000端口的请求"); while(true){ socket=server.accept(); if(socket!=null){ new Thread(new TestReveiveThread(socket)).start(); } } }catch (Exception e) { System.out.println("异常"); } } } class TestReveiveThread implements Runnable{ Socket socket; public TestReveiveThread(Socket s) { socket=s; } public void run() { try{ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8")); OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream(),"utf-8"); String line=null; while((line=bufferedReader.readLine())!=null){ System.out.println(line); if(line.equals("")){ break; } } //模拟http请求到网站,然后把内容转发给当前的http请求 // String path = "/"; // String host = "www.oschina.net"; // int port = 80; // Socket socket2 = new Socket(host, port); // OutputStreamWriter osw2 = new OutputStreamWriter(socket2.getOutputStream(),"utf-8"); // osw2.write("GET " + path + " HTTP/1.1 "); // osw2.write("Host: " + host + " "); // osw2.write(" "); // osw2.flush(); // BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(socket2.getInputStream(), "utf-8")); // String line2 = null; // while ((line2 = bufferedReader2.readLine()) != null) { // osw.write(line2+" "); // } // bufferedReader2.close(); // osw2.close(); // socket2.close(); osw.write("HTTP/1.1 200 OK "); osw.write("Server: Apache-Coyote/1.1 "); osw.write("Set-Cookie: JSESSIONID=03493794995CE31A0F131787B6C6CBB2; Path=/; HttpOnly "); osw.write("Content-Type: text/html;charset=UTF-8 "); osw.write("Transfer-Encoding: chunked "); osw.write("Date: Tue, 19 May 2015 02:48:27 GMT "); osw.write(" "); osw.write("c9 "); osw.write("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> "); osw.write("<HTML> "); osw.write(" <HEAD><TITLE>A Servlet</TITLE></HEAD> "); osw.write(" <BODY> "); osw.write(" This is class com.serv.myServ, using the GET method "); osw.write(" </BODY> "); osw.write("</HTML> "); osw.write(" "); osw.write("0"); osw.write(" "); osw.write(" "); osw.flush(); bufferedReader.close(); osw.close(); socket.close(); }catch (Exception e) { System.out.println("客户端接受异常"+e.getMessage()); } } }
在浏览器中输入 localhost:50000