Java网络编程实例---------------------------------Java HTTP代理程序
在Internet设置代理服务器为127.0.0.1:8000 其他浏览器使用IE设置
package org.bq; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.StringTokenizer; /** * 用Webkit内核浏览器可以运行成功,但是IE却有时候会出现问题 * 返回到浏览器端的HTML不能正确显示,但的确是返回了 * @author 白强 * @version 1.0 2013-11-19 * */ public class Proxy { public Proxy() throws IOException { ServerSocket server = new ServerSocket(8000); int socketNo = 1; while (true) { Socket socket = server.accept(); System.out.println("----Start For socket No" + socketNo + "------"); System.out.println("The socket name =======" + socket.getInetAddress().getHostName()); ProxtThread task = new ProxtThread(socket); new Thread(task).start(); socketNo++; } } public static void main(String[] args) throws Exception { new Proxy(); } } class ProxtThread implements Runnable{ private Socket socket; public ProxtThread(Socket socket) { this.socket=socket; } @Override public void run() { String reqs = ""; String hostName = ""; Integer port = 80; try{ // 定义从浏览器得到的请求信息Reader BufferedReader serverReader = new BufferedReader(new InputStreamReader( socket.getInputStream())); // 向浏览器返回数据 PrintWriter serverWriter = new PrintWriter(socket.getOutputStream()); String request = ""; // 得到请求的首行 request = serverReader.readLine(); System.out.println(request); // 对首行进行分析,以空格为界拆分字符串 StringTokenizer st = new StringTokenizer(request, " "); reqs = st.nextToken(); System.out.println(reqs); // 得到请求URL String http = st.nextToken(); String hostWithPort; if (http.startsWith("http://")) { // 子串 hostWithPort = http.substring(7, http.indexOf("/", 7)); // 如果有冒号就是指定端口拆分 if (hostWithPort.indexOf(":") != -1) { StringTokenizer stPort = new StringTokenizer(hostWithPort, ":"); hostName = stPort.nextToken(); port = new Integer(stPort.nextToken()); } // 不然就是80了 else { hostName = hostWithPort; port = 80; } // 打印请求URL System.out.println(hostName); } /* * Ready,可以向Internet发送数据了 */ Socket client = new Socket(hostName, port); PrintWriter clientWriter = new PrintWriter(client.getOutputStream()); //我试过www.nwpu.edu.cn;www.baidu.com;www.163.com BufferedReader clientReader = new BufferedReader(new InputStreamReader( client.getInputStream())); System.out.println("write to socket:" + request); int count = 0; while ((request != null) && (count < 8)) { System.out.println(request); count++; if (request.contains("Accept-Encoding:")) { System.out.println("contain:" + request); } else clientWriter.println(request); request = serverReader.readLine(); } clientWriter.println("\r\n"); clientWriter.flush(); // 接受得到Response String response = new String(); response = clientReader.readLine(); System.out.println(response); while (!(response.equals(""))) { System.out.println(response); response = clientReader.readLine(); } System.out.println(response); System.out.println("Start Receiving HTML ..."); while ((response = clientReader.readLine()) != null) { serverWriter.println(response); } serverWriter.flush(); System.out.println("Finished!!!"); } catch (Exception e) { e.printStackTrace(); } } }