派生类java实现http代理服务

http代理  时间:2021-03-03  阅读:()

j av a实现http代理服务public void writeLog(byte[]bytes, int offset, int len,boolean browser)throws IOExc eption{for(int i=0; i len; i++)writeLog((int)bytes[offs et+i],brow s er);

//默认情况下 日志信息输出到

//标准输出设备

//派生类可以覆盖它public String processHostName(String url,String host, int port,Socket sock) {java.text.DateFormat cal=java.text.DateFormat.getDateTimeInstance();System.out.println(c al.format(new java.util.Date())+-+url+

+sock.getInetAddres s()+\nreturn host;

//执行操作的线程public void run() {

String line;

String host;int port=80;

S oc ket outbound=null;try{s ocket.s etSoTimeout(TIMEOUT);

InputStream is=s oc ket.getInputStream();

OutputStream os=null;

try{

//获取请求行的内容line=host=int state=0;boolean space;w hile(true) {int c=is.read();if(c==-1)break;if(logging)wr iteLo g(c, true);spac e=Character.isWhitespac e((char)c);sw itch(state) {case0:if(space)c ontinue;state=1;case 1:if(space) {state=2;c ontinue;

line=line+(char)c;break;case2:if(space)c ontinue; //跳过多个空白字符state=3;case3:if(space) {state=4;

//只取出主机名称部分

String host0=host;int n;n=host.indexOf( //if(n!=-1)host=host.substring(n+2);n=host.indexOf(‘/’);if(n!=-1)host=host.substring(0,n);

//分析可能存在的端口号n=host.indexOf( :if(n!=-1) {port=Integer.parseInt(host.substring(n+1));

host=host.substring(0,n);host=processHostName(host0,host,port, socket);if(p arent !=null) {hos t=parent;port=parentP ort;int retry=CONNECT_RETRI ES;while(retry-- !=0) {try{outbound=new S oc ket(host,port);break;

} catch(Exc eption e) {

//等待

Thread.s leep(CONNECT_PAUSE);if(o utb o un d==nu ll)break;outbound.s etS oT imeout(T IMEOUT);o s=outb ound.getOutput S tream();os.write(line.getBytes());os.write(‘ ‘);os.write(host0.getBytes());os.write(‘ ‘);p ip e(is,outb ound.getInputS tream(),o s,

s oc ket.getOutputS tream());break;host=host+(char)c;break;

} catch(IOException e) {

} catch(Exc eption e) {

}final ly{try{socket.close();

} catch(Exception e1) {try{outbound.c lo s e();

} catch(Exc eption e2) {void pipe(InputStream is0, InputStream is 1,OutputStream os0,OutputStream os 1) throws IOException{try{int ir;byte bytes[]=new byte[BUFSIZ];w hile(true) {try{if((ir=is 0.read(bytes))0) {os0.write(bytes,0, ir);

if(logging)wr iteLo g(byte s,0, ir, true);

} else if(ir 0)break;

} catch(InterruptedIOExc eption e) {try{if((ir=is 1.read(bytes))0) {os 1.write(bytes,0, ir);if(logging)writeLo g(byte s,0, ir, fals e);

} else if(ir 0)break;

} catch(InterruptedIOExc eption e) {

} catch(Exception e0) {

System.out.println(Pipe异常:+e0);static public void startProxy(int port,Class clobj) {ServerSocket ssock;try{ssock=new ServerSocket(port);w hile(true) {

Class[] sarg=new Class[1];

Object[] arg=new Object[1];

sarg[0]=Socket.clas s;try{java.lang.reflect.Constructor cons=clobj

.getDeclaredConstructor(sarg);arg[0]=ssock.accept();c ons.newInstanc e(arg); //创建HttpProxy或其派生类的实例} catch(Exc eption e) {

Socket esock=(Socket)arg[0];try{esock.close();

} catch(Exception ec) {

} catch(IOException e) {static public void stopProxy(){try{if(null!=HttpProxy.s oc ket)

HttpProxy.socket.close();

} catch(IOException e) {e.printStackTrac e();

//测试用的简单main方法static public void main(String args[]) {

Sys tem.out.println(在端口808启动代理服务器\n

HttpProxy.lo g=S ys tem.out;

HttpProxy.lo gging=fals e;

HttpProxy.s tartProxy(808,HttpProxy.c las s);}

来自 http://c henyu-hz.iteye.c om/b lo g/1493227

Hostodo商家提供两年大流量美国VPS主机 可选拉斯维加斯和迈阿密

Hostodo商家算是一个比较小众且运营比较久的服务商,而且还是率先硬盘更换成NVMe阵列的,目前有提供拉斯维加斯和迈阿密两个机房。看到商家这两年的促销套餐方案变化还是比较大的,每个月一般有这么两次的促销方案推送,可见商家也在想着提高一些客户量。毕竟即便再老的服务商,你不走出来让大家知道,迟早会落寞。目前,Hostodo有提供两款大流量的VPS主机促销,机房可选拉斯维加斯和迈阿密两个数据中心,且都...

数脉科技香港物理机 E3 16G 10M 华为线路165元 阿里云线路 188元 Cera线路 157元

2021年9月中秋特惠优惠促销来源:数脉科技 编辑:数脉科技编辑部 发布时间:2021-09-11 03:31尊敬的新老客户:9月优惠促销信息如下,10Mbps、 30Mbps、 50Mbps、100Mbps香港优质或BGPN2、阿里云线路、华为云线路,满足多种项目需求!支持测试。全部线路首月五折起。数脉官网 https://my.shuhost.com/香港特价数脉阿里云华为云 10MbpsCN...

无忧云:洛阳/大连BGP云服务器38.4元/月,雅安物理机服务器315元/月起,香港荃湾CN2限时5折优惠

无忧云怎么样?无忧云是一家成立于2017年的老牌商家旗下的服务器销售品牌,现由深圳市云上无忧网络科技有限公司运营,是正规持证IDC/ISP/IRCS商家,主要销售国内、中国香港、国外服务器产品,线路有腾讯云国外线路、自营香港CN2线路等,都是中国大陆直连线路,非常适合免备案建站业务需求和各种负载较高的项目,同时国内服务器也有多个BGP以及高防节点,目前商家开启了夏日清凉补贴活动,商家的机器还是非常...

http代理为你推荐
视频截图软件视频截图软件手机游戏排行榜2015求一款2015人气最多的手游,最好有前十排行榜推广方法现在最常用的推广方式有哪几种安装程序配置服务器失败安装用友u8的数据库最后说:“安装程序配置服务器失败。参考服务器错误日志和 C:WINDOWSsqlstp.log 了解更多信息。”bluestacksbluestacks怎么用?不兼容vivo手机和软件不兼容怎么办?彩信中心短信中心号码是多少iphone越狱后怎么恢复已经越狱的iphone怎么恢复到原来的系统怎么升级ios6苹果6怎么升级最新系统免费qq空间装扮有办法免费装扮QQ空间吗??
com域名空间 安徽双线服务器租用 贝锐花生壳域名 GGC 分销主机 秒解服务器 私服服务器 网络星期一 godaddy域名转出 标准机柜尺寸 qingyun 工作站服务器 hinet adroit 网通服务器托管 带宽租赁 ebay注册 lamp的音标 nnt cdn加速技术 更多