java实现http代理服务详解编程语言
2023-06-13 09:20:29 时间
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 processHostName(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;
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 = processHostName(host0, host, port, socket);
if (parent != null) {
host = parent;
port = parentPort;
int retry = CONNECT_RETRIES;
while (retry-- != 0) {
try {
outbound = new Socket(host, port);
break;
} catch (Exception e) {
// 等待
Thread.sleep(CONNECT_PAUSE);
if (outbound == null)
break;
outbound.setSoTimeout(TIMEOUT);
os = outbound.getOutputStream();
os.write(line.getBytes());
os.write( );
os.write(host0.getBytes());
os.write( );
pipe(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 pipe(InputStream is0, InputStream is1, OutputStream os0,
OutputStream os1) throws IOException {
try {
int ir;
byte bytes[] = new byte[BUFSIZ];
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 (InterruptedIOException e) {
try {
if ((ir = is1.read(bytes)) 0) {
os1.write(bytes, 0, ir);
if (logging)
writeLog(bytes, 0, ir, false);
} else if (ir 0)
break;
} catch (InterruptedIOException e) {
} catch (Exception e0) {
System.out.println("Pipe异常: " + e0);
static public void startProxy(int port, Class clobj) {
ServerSocket ssock;
try {
ssock = new ServerSocket(port);
while (true) {
Class[] sarg = new Class[1];
Object[] arg = new Object[1];
sarg[0] = Socket.class;
try {
java.lang.reflect.Constructor cons = clobj
.getDeclaredConstructor(sarg);
arg[0] = ssock.accept();
cons.newInstance(arg); // 创建HttpProxy或其派生类的实例
} catch (Exception e) {
Socket esock = (Socket) arg[0];
try {
esock.close();
} catch (Exception ec) {
} catch (IOException e) {
static public void stopProxy(){
try {
if(null!=HttpProxy.socket)
HttpProxy.socket.close();
} catch (IOException e) {
e.printStackTrace();
// 测试用的简单main方法
static public void main(String args[]) {
System.out.println("在端口808启动代理服务器/n");
HttpProxy.log = System.out;
HttpProxy.logging = false;
HttpProxy.startProxy(808, HttpProxy.class);
}
来自:http://chenyu-hz.iteye.com/blog/1493227
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10781.html
cjava相关文章
- java启动器_JAVA基础:Java 启动器如何查找类
- java语言的平台无关性是指什么,《深入Java虚拟机》学习笔记二:平台无关性
- java有什么作用_Java有什么用「建议收藏」
- MySQL字段类型如何转为java_Java JDBC中,MySQL字段类型到JAVA类型的转换
- native2ascii java_Native2Ascii和Ascii2Native的Java实现
- Java 零拷贝_java clone 深拷贝
- java压缩与解压(Java.util.zip)详解编程语言
- Java通过http请求保存远程图片至本地详解编程语言
- 运用Java进行Http表单数据自动提交详解编程语言
- Java处理HTTP请求的相关代码详解编程语言
- Java 发送http post 请求详解编程语言
- Java学习笔记之九java二维数组及其多维数组的内存应用拓展延伸详解编程语言
- Java学习笔记之七java函数的语法规则总结详解编程语言
- Java学习笔记之二java标识符命名规范详解编程语言
- JAVA用http协议GET方法从服务器获取图片保存到本地详解编程语言
- java http post上传文件详解编程语言
- java 标准输出与标准错误 out与 err 区别 用法 联系 java中的out与err区别 System.out和System.err的区别 System.out.println和System.err.println的区别 Java重定向System.out和System.err详解编程语言
- .NET/ASP.NET/C#/WCF/SQL Server/My SQL/Java/JSP/JDBC/Spring/Spring MVC/PHP/Python/Ruby/Shell/Agile/CSS/HTML/HTTP/Unix/Linux大量PDF书籍/电子书籍下载, Effective Java 下载详解编程语言
- Linux平台上Java新版本发布(linux发布java)
- Java http数据MD5、AES、DES加密详解编程语言
- 服务器实现Java远程访问Linux服务器(java远程linux)
- Java连接MySQL:实现数据库操作(java中连接mysql)
- 如何在Linux系统中正确配置Java?(linux下配置java)
- Java实现Redis数据存储(java的redis)
- 使用Java连接SQL Server数据库,轻松实现数据交互(java连sqlserver)
- Java实现Linux:跨平台解决方案(java 实现linux)
- Linux上善用Java:实现智能开发(linux 运行java)
- 使用java实现http多线程断点下载文件(二)
- java实现http请求工具类示例