Java:在我的ProxyServer中处理浏览器的请求

时间:2014-03-04 11:04:28

标签: java sockets proxy

我正在撰写ProxyServer,首先我使用ServerSocket来获取与我相关的Socket。然后,我从已连接的InputStream获取Socket并加入其中。我已将浏览器的请求输出到控制台:

GET http://mail.ru/ HTTP/1.1
Host: mail.ru
Proxy-Connection: keep-alive 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/31.0.1650.63 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: p=7nkAAOS6hAAA; mrcu=7D86524D326C5CB36F898F4A4CD9;
_ga=GA1.2.1174941236.1388398432; c=PCwPUwAAAPKbAAAiAgAAcQAAz5QIAgAA;

我必须创建新的Socket才能将此请求写入Internet,但Socket需要端口号和主机名(String)才能连接。

如果我们认为端口号始终为80,那么从该请求的哪个部分我必须获得主机名?这是GET http://mail.ru/ HTTP/1.1还是Host: mail.ru

另外,我只能发送到互联网GET http://mail.ru/ HTTP/1.1放弃其他部分吗?

最后一个问题是,我是否应该重写此GET方法本身?

1 个答案:

答案 0 :(得分:0)

我创建了最简单的ProxyServer。它的工作速度不是很快,但有效:)

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;



/**
 *
 * @author 
 */
public class DaniyarProxyServer {
    public static final int port = 8000;

public static void main(String[] args) {
    ServerSocket listener = null;

    try {
        listener = new ServerSocket(port);
        System.out.println("Proxy started..");
        while (true) {
            try {
                Socket client = listener.accept();
                ClientHandler handler = new ClientHandler(client);
            } catch (Exception e) {
                System.err.println("Error: " + e.getMessage());
            }
        }
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
}
}

class ClientHandler implements Runnable {

private Socket client;
private Socket internet;

private Thread thread;

public static int threadcount = 0;

private static int buffer_size = 8192;

private static String CRLF = "\r\n";

public int port = 80;

public ClientHandler(Socket client) {
    this.client = client;
    thread = new Thread(this, "Thread#" + threadcount);
    System.out.println("Thread#" + threadcount + " is running now");
    threadcount++;
    thread.run();
}

@Override
public void run() {
    try {
        byte[] buffer = new byte[buffer_size];
        BufferedInputStream clientIstream = new BufferedInputStream(client.getInputStream());
        clientIstream.read(buffer);

        String clientRequest = new String(buffer);

        System.out.println(clientRequest);

        String host = getHostFromRequest(clientRequest);

        internet = new Socket(host, port);
        BufferedOutputStream internetOstream = new BufferedOutputStream(internet.getOutputStream());
        internetOstream.write(clientRequest.getBytes());
        internetOstream.flush();

        BufferedOutputStream clientOstream = new BufferedOutputStream(client.getOutputStream());

        BufferedInputStream internetIstream = new BufferedInputStream(internet.getInputStream());

        buffer = new byte[buffer_size];
        int buffsize = 0;
        while ((buffsize = internetIstream.read(buffer)) != -1) {
            clientOstream.write(buffer, 0, buffsize);
        }
        clientOstream.flush();

        clientIstream.close();
        clientOstream.close();
        internetIstream.close();
        internetOstream.close();

    } catch (Exception e) {
        System.err.println("Exception " + e.getMessage());
    }
}


private String getHostFromRequest(String request) {
    String host = "";

    StringTokenizer tok = new StringTokenizer(request, CRLF);
    String method = tok.nextToken();
    host = tok.nextToken();

    host = host.substring("Host: ".length());

    return host;
}
}