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

hosthatch:14个数据中心15美元/年

hosthatch在做美国独立日促销,可能你会说这操作是不是晚了一个月?对,为了准备资源等,他们拖延到现在才有空,这次是针对自己全球14个数据中心的VPS。提前示警:各个数据中心的网络没有一个是针对中国直连的,都会绕道而且ping值比较高,想买的考虑清楚再说!官方网站:https://hosthatch.com所有VPS都基于KVM虚拟,支持PayPal在内的多种付款方式!芝加哥(大硬盘)VPS5...

恒创科技SonderCloud,美国VPS综合性能测评报告,美国洛杉矶机房,CN2+BGP优质线路,2核4G内存10Mbps带宽,适用于稳定建站业务需求

最近主机参考拿到了一台恒创科技的美国VPS云服务器测试机器,那具体恒创科技美国云服务器性能到底怎么样呢?主机参考进行了一番VPS测评,大家可以参考一下,总体来说还是非常不错的,是值得购买的。非常适用于稳定建站业务需求。恒创科技服务器怎么样?恒创科技服务器好不好?henghost怎么样?henghost值不值得购买?SonderCloud服务器好不好?恒创科技henghost值不值得购买?恒创科技是...

iWebFusion:独立服务器月付57美元起/5个机房可选,10Gbps服务器月付149美元起

iWebFusion(iWFHosting)在部落分享过很多次了,这是成立于2001年的老牌国外主机商H4Y旗下站点,提供的产品包括虚拟主机、VPS和独立服务器租用等等,其中VPS主机基于KVM架构,数据中心可选美国洛杉矶、北卡、本德、蒙蒂塞洛等。商家独立服务器可选5个不同机房,最低每月57美元起,而大流量10Gbps带宽服务器也仅149美元起。首先我们分享几款常规服务器配置信息,以下机器可选择5...

http代理为你推荐
无线路由器限速设置无线路由器怎么设置限速网站联盟怎样进入网站联盟网站联盟百度网盟是什么,怎么加入显卡温度多少正常电脑显卡温度多少正常?今日热点怎么删除怎样删除实时热点奇虎论坛奇虎问答是什么mate8价格现在买华为mate8高配划算吗分词技术怎么在SEO中学会运用关键词分词技术怎么上传音乐怎么上传音乐到网上发邮件怎么发发邮件怎么发?
厦门域名注册 wordpress主机 smartvps a2hosting sugarsync payoneer 京东云擎 hnyd 服务器托管什么意思 免费cdn 支持外链的相册 yundun 贵阳电信测速 腾讯网盘 睿云 中国电信宽带测速 建站技术 e-mail hosts文件 电信测速器在线测网速 更多