如何将HTTP请求转发回浏览器?代理Java

时间:2021-10-22 17:25:03
final int portNumber = 8128;
String str;

int start = 0;
int endSg = 0;
int endCom = 0;

String ReqWeb=null;

System.out.println("Creating server socket on port " + portNumber);
ServerSocket serverSocket = new ServerSocket(portNumber);

BufferedReader inFromServer;
OutputStream out;
PrintWriter outw;   
Socket forwardSocket = null;

while (true)
{
    Socket socket = serverSocket.accept();              //get client request
    String from = socket.getInetAddress().toString();
    System.out.println("Accepted connection from " + from);
    OutputStream os = socket.getOutputStream();
    PrintWriter pw = new PrintWriter(os, true);
    pw.println("What's your request?");

    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    //System.out.println(str);
    while((str = br.readLine())!=null){
        System.out.println(str);     
        if(str!=null){
            start = str.indexOf("Host: ") + 6;
            endSg = str.indexOf(".sg", start) + 3;
            endCom = str.indexOf(".com", start) + 4;
            if(((endSg>3)||(endCom>4))&&(start>4)){
                if(endSg>3)
                    ReqWeb = str.substring(start, endSg);
                else if(endCom>3)
                    ReqWeb = str.substring(start, endCom);              
            }

        }
    }
    System.out.println(ReqWeb);

    if(ReqWeb!=null){
        //ReqWeb = str.substring(start);
        System.out.println(ReqWeb);
        forwardSocket = new Socket(ReqWeb, 80);
    }

    pw.println(str);
    pw.println(ReqWeb);     
    //socket.close();

    if(forwardSocket!=null){
        inFromServer = new BufferedReader(new InputStreamReader(forwardSocket.getInputStream()));
        out = forwardSocket.getOutputStream();
        outw = new PrintWriter(out, false);
        outw.print(str);
    }

}

Output :

Creating server socket on port 8128
Accepted connection from /127.0.0.1
null
Accepted connection from /127.0.0.1
GET (http://)*.com/questions/12900825/how-do-i-forward-the-http-request-back-to-the-browser-proxy-java HTTP/1.0
Host: *.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:15.0) Gecko/20100101 Firefox/15.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: close
Proxy-Connection: close
Pragma: no-cache

now this is the output i got, should i store in a byte array to send it back to the server to request for the page? I am still having trouble forwarding this request ):

现在这是我得到的输出,我应该存储在一个字节数组中,将其发送回服务器请求页面?我仍然无法转发此请求):

1 个解决方案

#1


0  

You'll need to open another socket to the intended target (as per firefox's request) and send the request there. Keep the socket that's connected to firefox open because when you get the response from the intended target, you'll read it from the target and write it back to firefox. Depending on your pipelining settings in firefox, the connection either may then close or may make more requests.

您需要打开另一个套接字到目标目标(根据firefox的请求)并在那里发送请求。保持连接到firefox的套接字打开,因为当你从目标目标获得响应时,你将从目标中读取它并将其写回firefox。根据您在firefox中的管道设置,连接可能会关闭或可能会发出更多请求。

#1


0  

You'll need to open another socket to the intended target (as per firefox's request) and send the request there. Keep the socket that's connected to firefox open because when you get the response from the intended target, you'll read it from the target and write it back to firefox. Depending on your pipelining settings in firefox, the connection either may then close or may make more requests.

您需要打开另一个套接字到目标目标(根据firefox的请求)并在那里发送请求。保持连接到firefox的套接字打开,因为当你从目标目标获得响应时,你将从目标中读取它并将其写回firefox。根据您在firefox中的管道设置,连接可能会关闭或可能会发出更多请求。