派生类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

ZJI:韩国BGP+CN2线路服务器,国内三网访问速度优秀,8折优惠码每月实付440元起

zji怎么样?zji最近新上韩国BGP+CN2线路服务器,国内三网访问速度优秀,适用8折优惠码zji,优惠后韩国服务器最低每月440元起。zji主机支持安装Linux或者Windows操作系统,会员中心集成电源管理功能,8折优惠码为终身折扣,续费同价,全场适用。ZJI是原Wordpress圈知名主机商:维翔主机,成立于2011年,2018年9月启用新域名ZJI,提供中国香港、台湾、日本、美国独立服...

IonSwitch:$1.75/月KVM-1GB/10G SSD/1TB/爱达荷州

IonSwitch是一家2016年成立的国外VPS主机商,部落上一次分享的信息还停留在2019年,主机商提供基于KVM架构的VPS产品,数据中心之前在美国西雅图,目前是美国爱达荷州科德阿伦(美国西北部,西接华盛顿州和俄勒冈州),为新建的自营数据中心。商家针对新数据中心运行及4号独立日提供了一个5折优惠码,优惠后最低1GB内存套餐每月仅1.75美元起。下面列出部分套餐配置信息。CPU:1core内存...

物语云-VPS-美国洛杉矶VPS无限流量云windows大带宽100M不限流量 26/月起

物语云计算怎么样?物语云计算(MonogatariCloud)是一家成立于2016年的老牌国人商家,主营国内游戏高防独服业务,拥有多家机房资源,产品质量过硬,颇有一定口碑。本次带来的是特惠活动为美国洛杉矶Cera机房的不限流量大带宽VPS,去程直连回程4837,支持免费安装Windows系统。值得注意的是,物语云采用的虚拟化技术为Hyper-v,因此并不会超售超开。一、物语云官网点击此处进入物语云...

http代理为你推荐
万维读者网罂粟花的含义?推广方法营业推广的方式有哪些如何免费开通黄钻怎样才能免费开通黄钻人人时光机求 《小叮当》的日语歌词。最好有片假名的1433端口1433端口怎么打开怎么点亮qq空间图标QQ空间的图标怎么点亮宽带接入服务器互联网的接入方式有哪几种?声母是什么什么是声母,什么是音母?网站地图制作怎么制作网站地图啊,购买流量移动卡如何购买流量?
新网域名 域名管理 万网域名解析 秒解服务器 博客主机 isatap 火车票抢票攻略 ev证书 qq云端 广州服务器 中国电信宽带测速器 美国凤凰城 万网注册 汤博乐 美国asp空间 hosting cloudflare easypanel linuxvi 跟踪路由 更多