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

CYUN专注海外精品服务器资源 国庆钜惠 最低5折起 限量促销

国庆钜惠 最低5折起 限量促销CYUN专注海外精品服务器资源,主营香港CN2 GIA、美国CERA、美国高防服务器资源,实体公司,ISP/IDC资质齐全,客服配备齐全。本次针对国庆推出非常给力的促销活动,旗下所有平台同享,新老客户同享,限时限量,售完截止。活动截止时间:2021年10月9日官网地址:www.cyun.net参与机型:香港CN2 GIA云服务器、香港双程CN2云服...

云如故枣庄高防(49元)大内存2H2G49元8H8G109元

云如故是一家成立于2018年的国内企业IDC服务商,由山东云如故网络科技有限公司运营,IDC ICP ISP CDN VPN IRCS等证件齐全!合法运营销售,主要从事自营高防独立服务器、物理机、VPS、云服务器,虚拟主机等产品销售,适合高防稳定等需求的用户,可用于建站、游戏、商城、steam、APP、小程序、软件、资料存储等等各种个人及企业级用途。机房可封UDP 海外 支持策略定制 双层硬件(傲...

RFCHOST - 洛杉矶CN2 GIA VPS季付23.9美元起 100Mbps带宽

RFCHOST,这个服务商我们可能有一些朋友知道的。不要看官网是英文就以为是老外服务商,实际上这个服务商公司在上海。我们实际上看到的很多商家,有的是繁体,有的是英文,实际上很多都是我们国人朋友做的,有的甚至还做好几个品牌域名,实际上都是一个公司。对于RFCHOST商家还是第一次分享他们家的信息,公司成立大约2015年左右。目前RFCHOST洛杉矶机房VPS正进行优惠促销,采用CN2优化线路,电信双...

http代理为你推荐
印章制作刻章需要什么证件湖南商标注册湖南商标注册代办公司在线漏洞检测如果检测网站是否有漏洞?iphone越狱后怎么恢复苹果手机越狱后怎么恢复数据库损坏数据库坏了怎么办安装迅雷看看播放器迅雷看看不能播放,说我尚未安装迅雷看看播放器商标注册查询官网如何在网上查询商标是否注册?电子商务网站模板我想开发一个电子商务网站,但是想加入自己设计的模板,可以吗?发邮件怎么发发邮件怎么发?服务器连接异常lol为什么总是提示服务器连接异常
过期域名查询 俄罗斯vps 播放vps上的视频 个人域名备案流程 10t等于多少g 免费ftp空间 12306抢票助手 地址大全 最好的空间 小米数据库 135邮箱 广州服务器 傲盾官网 卡巴斯基破解版 申请网页 电信托管 江苏双线服务器 我的世界服务器ip 免费赚q币 时间同步服务器 更多