2014-03-04 92 views
0

我正在寫ProxyServer,首先我用ServerSocket來獲得連接到我的Socket。然後我從Socket得到InputStream,並將其付諸實施。我已經輸出我的瀏覽器的請求控制檯:Java:在我的代理服務器中處理瀏覽器的請求

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寫這個請求,互聯網,但是需要插座端口號和主機名(字符串)進行連接。

如果我們認爲端口號總是80那麼從這個請求的哪一部分我必須得到主機名?這是GET http://mail.ru/ HTTP/1.1還是Host: mail.ru

另外,我可以只發送到互聯網GET http://mail.ru/ HTTP/1.1放棄其他部分?

最後一個問題是,我是否必須重寫這個GET方法本身?

+0

如果它應該是HTTP代理,然後獲取HOST頭。並且您將所有數據傳遞給傳出流並將其複製回傳入流。順便說一句,你必須照顧分塊與瀏覽器有廣泛的兼容性。 –

+0

好吧,我會採取主機頭,我應該重寫GET http://mail.ru/ HTTP/1.1? –

+0

如何重寫?只需將輸入複製到目標輸出以獲取基本功能。 –

回答

0

我已經創建了simpliest 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; 
} 
} 
+0

有人使用過這段代碼嗎? – MadMad666

+0

這是我大學的實驗室工作。如果我記得,那很好。只需從電腦運行並檢查。 –

相關問題