简体   繁体   English

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

[英]How do i forward the HTTP request back to the browser? Proxy Java

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://)stackoverflow.com/questions/12900825/how-do-i-forward-the-http-request-back-to-the-browser-proxy-java HTTP/1.0
Host: stackoverflow.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 ): 我仍然无法转发此请求):

You'll need to open another socket to the intended target (as per firefox's request) and send the request there. 您需要打开另一个用于目标的套接字(根据Firefox的请求)并将请求发送到该目标。 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. 使连接到Firefox的套接字保持打开状态,因为当您从预期目标获得响应时,您将从目标读取响应并将其写回到Firefox。 Depending on your pipelining settings in firefox, the connection either may then close or may make more requests. 根据您在firefox中的流水线设置,连接可能会关闭或发出更多请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM