【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“TextHttpProxy.java”一文,供大家参考学习!
package com.zf.s13;
import java.lang.reflect.Constructor;
import java.net.*;
import java.io.*;
public class TextHttpProxy extends Thread {//操作实现代理服务器的类
static public int RETRIES = 5;//在放弃之前尝试连接远程主机的次数
static public int PAUSE = 5;//在两次连接尝试之间的暂停时间
static public int TIMEOUT = 50;//等待Socket输入的等待时间
static public int BUFSIZ = 1024;//输入的缓冲大小
static public boolean logging = false;//是否要求代理服务器在日志中记录所有已传输的数据
static public OutputStream log = null;//默认日志例程将向该OutputStream对象输出日志信息
protected Socket socket;// 传入数据用的Socket
static private String parent = null;//上级代理服务器
static private int parentPort = -1;
// 用来把一个代理服务器链接到另一个代理服务器(需要指定另一个服务器的名称和端口)。
static public void setParentProxy(String name, int port) {
parent = name;
parentPort = port;
}
public TextHttpProxy(Socket s) {//创建一个代理线程
socket = s;
start();//启动线程
}
public void writeLog(int c, boolean browser) throws IOException {//写日志
log.write(c);
}
public void writeLog(byte[] bytes, int offset, int len, boolean browser)
throws IOException {//循环写日志
for (int i = 0; i < len; i++)
writeLog((int) bytes[offset + i], browser);
}
// 默认情况下,日志信息输出到控制台或文件
public String printLog(String url, String host, int port, Socket sock) {
java.text.DateFormat cal = java.text.DateFormat.getDateTimeInstance();
System.out.println(cal.format(new java.util.Date()) + " - " + url + " "
+ sock.getInetAddress() + "\n");
return host;
}
public void run() {// 执行操作的线程
String line;
String host;
int port = 80;//默认端口为80
Socket outbound = null;
try {
socket.setSoTimeout(TIMEOUT);//设置超时时间
InputStream is = socket.getInputStream();//创建输入流
OutputStream os = null;
try {
line = "";// 获取请求行的内容
host = "";
int state = 0;
boolean space;
while (true) {//无限循环
int c = is.read();//读取输入流的信息
if (c == -1)//没有读取信息
break;
if (logging)
writeLog(c, true);//将信息写入日志
space = Character.isWhitespace((char) c);//判断是否为空白字符
switch (state) {//判断状态
case 0:
if (space)
continue;//跳过本次循环
state = 1;//更改状态
case 1:
if (space) {
state = 2;
continue;//跳过本次循环
}
line = line + (char) c;//添加读取的信息
break;
case 2:
if (space)
continue; // 跳过空白字符
state = 3;//更改状态
case 3:
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 = printLog(host0, host, port, socket);//获得网站域名
if (parent != null) {
host = parent;
port = parentPort;
}
int retry = RETRIES;
while (retry-- != 0) {
try {
outbound = new Socket(host, port);//创建连接对象
break;
} catch (Exception e) {
System.out.println("无法创建连接:"+e.getMessage());
}
Thread.sleep(PAUSE);//设置线程等待
}
if (outbound == null)
break;
outbound.setSoTimeout(TIMEOUT);//设置超时时间
os = outbound.getOutputStream();//获得输出流对象
os.write(line.getBytes());//将信息写入流
os.write(' ');
os.write(host0.getBytes());//将信息写入流
os.write(' ');
writeInfo(is, outbound.getInputStream(), os, socket
.getOutputStream());//调用方法将信息写入日志
break;
}
host = host + (char) c;
break;
}
}
} catch (IOException e) {
}
} catch (Exception e) {
} finally {
try {
socket.close();//释放资源
} catch (Exception e1) {
}
try {
outbound.close();
} catch (Exception e2) {
}
}
}
void writeInfo(InputStream is0, InputStream is1, OutputStream os0,
OutputStream os1) throws IOException {//读取流中信息写入日志
try {
int ir;
byte bytes[] = new byte[BUFSIZ];//创建字节数组,大小:1024
while (true) {
try {
if ((ir = is0.read(bytes)) > 0) {//判断读取输入流的信息
os0.write(bytes, 0, ir);//将读取的数据写入输出流对象中
if (logging)
writeLog(bytes, 0, ir, true);//写入日志
} else if (ir < 0)//读取完毕
break;
} catch (Interru