简单的java版http代理
为了说明http代理的工作机制,我特意写了一个最简单的代理,去掉很多繁杂的功能,并尽可能的使代码量小,源码如下,不清楚http代理机制的朋友参考一下基本就明白了。[java]/** $RCSfile: SimpleHttpProxy.java,v $$* $Revision: 1.1 $* $Date: 2013-1-9 $** Copyright (C) 2008 Skin, Inc. All rights reserved.** This software is the proprietary information of Skin, Inc.* Use is subject to license terms.*/package test.com.skin.http.proxy;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketTimeoutException;import java.net.URL;/*** <p>Title: SimpleHttpProxy</p>* <p>Description: </p>* <p>Copyright: Copyright (c) 2006</p>* @author xuesong.net* @version 1.0*/public class SimpleHttpProxy{public static final int PORT = 6666;public static final byte[] CRLF = new byte[]{0x0D, 0x0A};/*** @param args*/public static void main(String[] args){ServerSocket socketServer = null;try{socketServer = new ServerSocket(PORT);while(true){try{final Socket socket = socketServer.accept();(new Thread(){public void run(){SimpleHttpProxy.service(socket);}}).start();}catch(SocketTimeoutException e){e.printStackTrace();}catch(IOException e){e.printStackTrace();}}}catch(Exception e){e.printStackTrace();}finally{if(socketServer != null){try{socketServer.close();}catch(IOException e){}}}}private static void service(Socket socket){Socket remote = null;try{socket.setSoTimeout(2000);socket.setKeepAlive(false);InputStream inputStream = socket.getInputStream();OutputStream outputStream = socket.getOutputStream();/*** 读取协议头的第一行* 格式: GET http://www.mytest.com HTTP/1.1*/byte[] buffer = readLine(inputStream);if(buffer.length < 1){return;}String header = new String(buffer, "UTF-8");String[] action = header.split(" ");if(action.length < 3){return;}String address = action[1];/*** 目标地址是从http协议的第一行取* 目标主机应该从协议的Host头里面取,如果Host取不到, 从地址里面取&n补充:软件开发 , Java ,
上一个:反射机制详解
下一个:hibernate增删改查的标准范例
- 更多JAVA疑问解答:
- java怎么在线读取ftp服务器上的文件内容
- 关于程序员的职业规划
- HTML和JSP矛盾吗?
- java小程序如何打包?
- java怎么split路径文件名?
- jsp+javaBean中Column 'ordersPrice' specified twice的错误
- Java TCP/IP Socket网络编程系列
- 大家来讨论一下我到底该用什么好?Swing 还是 JavaFX
- 关于Hibernate实体自身多对一的抓取问题
- 关于apache2+tomcat群集出现的问题
- spring 获取上下文问题
- SSH 导入导出excel 谁有这块的资料吗?
- Ext TreePanel 刷新问题
- springmvc 加载一个jsp页面执行多个方法 报404
- checkbox数组action怎么向页面传值