要实现的功能很简单,访问的网站的时候,用其他的端口来代理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();

}}

解决方案 »

  1.   

    怎么没看到你解析http信息的? 或者你只是想做一个转发? 把过来的请求转发给80端口?如果是转发的话,接到请求直接转发请求就可以了。可以用httpconnection试一试
      

  2.   

    这个程序的主要功能是在计算机上面另开一个端口,把原来在80端口发出去的http请求转到其他端口上面发送给web服务器,或许我这个标题没有拟好!好比:这个程序相当于一个中介,把浏览器发出的请求给这个中介,再由这个中介通过不同于80的端口发送给服务器,服务器受到请求以后将请求的页面发送给这个中介,再由这个中介转发给浏览器!不过这里没有用到浏览器,是直接将服务器发过来页面的html代码显示在控制台,我的问题就在于:这个中介接收浏览器发过来的http信息在转发,这个过程中出现了一些小问题,导致中介再发给服务器的时候,“Bad Request!”,如果还不明白我的疑问,可以给我留言~感谢兄台的上心!