要实现的功能很简单,访问的网站的时候,用其他的端口来代理80端口
这个程序相当于一个http代理服务器,我小菜,不知道怎么描述,大侠们应该能明白我所要表达的意思吧
废话不多说,直接来代码,我想要的效果就是能够不报错的运行起来,先谢过了!
package org.bruce.sunone.http_proxy_server;import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;//代理服务的线程服务类
public class HttpProxy extends Thread { //尝试对远程连接的次数
public static int CONNECT_RETRIES = 5;
//间隔多少时间尝试一次对远程连接
public static int CONNECT_PAUSE = 5;
//等待用户从浏览器发布数据的时间
public static int TIMEOUT = 500;
//Socket的输入缓冲
public static int BUFSIZ = 1024;
//log标识
public static boolean logging = true;
//log信息的输出流
public static OutputStream log = null;
public String head = "";
protected Socket Socket;
//初始化并启动代理服务
public HttpProxy(Socket s) {
Socket = s;
}
//写日志信息
public void writeLog(byte[] bytes, int offset, int len) throws IOException {
for(int i = offset; i < len; i ++) {
log.write((int)bytes[i]);
}
}
public void run() {
String line;
String host;
//80端口
int port = 80;
Socket outbound = null;
try {
//超时时间为TIMEOUT毫秒
Socket.setSoTimeout(TIMEOUT);
//得到Socket输入流
InputStream is = Socket.getInputStream();
host = getHost(is);
//OutputStream os = Socket.getOutputStream();
try {
while(true) {
int n = -1;
n = host.indexOf(":");
if(n != -1) {
port = Integer.parseInt(host.substring(n+1));
host = host.substring(0, n);
}
int retry = CONNECT_RETRIES;
// System.out.println("host is " + host);
//连接重试
while(retry-- != 0) {
try {
//和远程主机建立连接
outbound = new Socket(host, port);
break;
} catch(Exception e) {
e.printStackTrace();
}
//没间隔一段时间,尝试一次连接
Thread.sleep(CONNECT_PAUSE);
}
if(outbound == null) {
break;
}
outbound.setSoTimeout(TIMEOUT);
OutputStream os1 = outbound.getOutputStream();
byte[] bytes = head.getBytes();
os1.write(bytes, 0, bytes.length);
pipe(Socket.getInputStream(), outbound.getInputStream(), Socket.getOutputStream(), os1);
break;
}
} catch(IOException e) {
}
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
Socket.close();
outbound.close();
} catch(Exception e1) {
e1.printStackTrace();
}
}
}
public String getHost(InputStream is) throws IOException {
String host = null;
DataInputStream dis = new DataInputStream(is);
//jdk.13API中readLine方法已经被BufferedReader替换
host = dis.readLine();
// System.out.println(host);
head = host;
System.out.println(head);
int n = host.indexOf("//");
if(n != -1) {
host = host.substring(n + 2);
}
n = host.indexOf('/');
if(n != -1) {
//FSF#W@#$@#$R#%$@#TT@#@T@#$$@##R#R
host = host.substring(0, n);
}
// System.out.println("leave getHost:" + host);
return host;
}
void pipe(InputStream is0, InputStream is1, OutputStream os0, OutputStream os1) throws IOException {
try {
int i;
byte[] bytes = new byte[BUFSIZ];
while(true) {
try {
//将客户端输入流写入服务器端输出流
//如果 inputstream 没有数据则方法阻塞
if((i = is0.read(bytes)) > 0) {
os1.write(bytes, 0, i);
if(logging) {
writeLog(bytes, 0, i);
}
} else if(i < 0) {
break;
}
} catch(InterruptedIOException e1) {
e1.printStackTrace();
System.exit(1);
}
try {
//将服务端输入流写入客户端输出流
//如果 inputstream 没有数据,read方法阻塞
if((i = is1.read(bytes)) > 0) {
os0.write(bytes, 0, i);
if(logging) {
writeLog(bytes, 0, i);
} else if(i < 0) {
break;
}
}
} catch(InterruptedIOException e) {
e.printStackTrace();
// System.exit(1);
}
}
}catch(Exception e) {
System.out.println("Piepe Exception:" + e);
e.printStackTrace();
}
}
public static void main(String[] args) {
int port = 8887;
System.out.println("Starting proxy on port " + port);
HttpProxy.log = System.out;
HttpProxy.logging = true;
ServerSocket ssock;
Socket sock;
try {
ssock = new ServerSocket(port);
while(true) {
sock = ssock.accept();
(new HttpProxy(sock)).start();
}
} catch(Exception e) {
e.printStackTrace();
}
}}
这个程序相当于一个http代理服务器,我小菜,不知道怎么描述,大侠们应该能明白我所要表达的意思吧
废话不多说,直接来代码,我想要的效果就是能够不报错的运行起来,先谢过了!
package org.bruce.sunone.http_proxy_server;import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;//代理服务的线程服务类
public class HttpProxy extends Thread { //尝试对远程连接的次数
public static int CONNECT_RETRIES = 5;
//间隔多少时间尝试一次对远程连接
public static int CONNECT_PAUSE = 5;
//等待用户从浏览器发布数据的时间
public static int TIMEOUT = 500;
//Socket的输入缓冲
public static int BUFSIZ = 1024;
//log标识
public static boolean logging = true;
//log信息的输出流
public static OutputStream log = null;
public String head = "";
protected Socket Socket;
//初始化并启动代理服务
public HttpProxy(Socket s) {
Socket = s;
}
//写日志信息
public void writeLog(byte[] bytes, int offset, int len) throws IOException {
for(int i = offset; i < len; i ++) {
log.write((int)bytes[i]);
}
}
public void run() {
String line;
String host;
//80端口
int port = 80;
Socket outbound = null;
try {
//超时时间为TIMEOUT毫秒
Socket.setSoTimeout(TIMEOUT);
//得到Socket输入流
InputStream is = Socket.getInputStream();
host = getHost(is);
//OutputStream os = Socket.getOutputStream();
try {
while(true) {
int n = -1;
n = host.indexOf(":");
if(n != -1) {
port = Integer.parseInt(host.substring(n+1));
host = host.substring(0, n);
}
int retry = CONNECT_RETRIES;
// System.out.println("host is " + host);
//连接重试
while(retry-- != 0) {
try {
//和远程主机建立连接
outbound = new Socket(host, port);
break;
} catch(Exception e) {
e.printStackTrace();
}
//没间隔一段时间,尝试一次连接
Thread.sleep(CONNECT_PAUSE);
}
if(outbound == null) {
break;
}
outbound.setSoTimeout(TIMEOUT);
OutputStream os1 = outbound.getOutputStream();
byte[] bytes = head.getBytes();
os1.write(bytes, 0, bytes.length);
pipe(Socket.getInputStream(), outbound.getInputStream(), Socket.getOutputStream(), os1);
break;
}
} catch(IOException e) {
}
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
Socket.close();
outbound.close();
} catch(Exception e1) {
e1.printStackTrace();
}
}
}
public String getHost(InputStream is) throws IOException {
String host = null;
DataInputStream dis = new DataInputStream(is);
//jdk.13API中readLine方法已经被BufferedReader替换
host = dis.readLine();
// System.out.println(host);
head = host;
System.out.println(head);
int n = host.indexOf("//");
if(n != -1) {
host = host.substring(n + 2);
}
n = host.indexOf('/');
if(n != -1) {
//FSF#W@#$@#$R#%$@#TT@#@T@#$$@##R#R
host = host.substring(0, n);
}
// System.out.println("leave getHost:" + host);
return host;
}
void pipe(InputStream is0, InputStream is1, OutputStream os0, OutputStream os1) throws IOException {
try {
int i;
byte[] bytes = new byte[BUFSIZ];
while(true) {
try {
//将客户端输入流写入服务器端输出流
//如果 inputstream 没有数据则方法阻塞
if((i = is0.read(bytes)) > 0) {
os1.write(bytes, 0, i);
if(logging) {
writeLog(bytes, 0, i);
}
} else if(i < 0) {
break;
}
} catch(InterruptedIOException e1) {
e1.printStackTrace();
System.exit(1);
}
try {
//将服务端输入流写入客户端输出流
//如果 inputstream 没有数据,read方法阻塞
if((i = is1.read(bytes)) > 0) {
os0.write(bytes, 0, i);
if(logging) {
writeLog(bytes, 0, i);
} else if(i < 0) {
break;
}
}
} catch(InterruptedIOException e) {
e.printStackTrace();
// System.exit(1);
}
}
}catch(Exception e) {
System.out.println("Piepe Exception:" + e);
e.printStackTrace();
}
}
public static void main(String[] args) {
int port = 8887;
System.out.println("Starting proxy on port " + port);
HttpProxy.log = System.out;
HttpProxy.logging = true;
ServerSocket ssock;
Socket sock;
try {
ssock = new ServerSocket(port);
while(true) {
sock = ssock.accept();
(new HttpProxy(sock)).start();
}
} catch(Exception e) {
e.printStackTrace();
}
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货