Java中的代理 - 客户端不显示响应?

时间:2013-10-06 10:03:19

标签: java sockets http proxy

我的编程任务要求我在客户端和服务器之间创建代理。 我的客户端通过代理向服务器发出请求,然后代理转发它并将响应从服务器返回给客户端。

以下是我采取的步骤:

1)从客户端获取请求并将请求数据存储到字节数组

2)使用缓冲读取器从字节数组中读取

3)从Host:header字段获取主机名,并用它创建一个serverSocket

4)将请求数据转发到serverSocket输出流

5)将来自serverSocket输入流的响应数据检索到另一个字节数组

6)将字节数组中的内容写入clientSocket输出流

但是,在步骤6之后,浏览器无法显示响应数据。有什么帮助吗?

import java.io.*;
import java.net.*;
import java.util.*;

public class proxy2 {

  public static void main(String args[]) throws Exception
  {
    int port = Integer.parseInt(args[0]);
    File fileSub= new File(args[1]); //fileSub
    File fileRediect = new File(args[2]); //fileRedirect

    ServerSocket listener = new ServerSocket(port);


    while (true) {
      proxy_func(listener.accept());

    }

  }

  public static void proxy_func(Socket clientSocket) throws Exception{

    OutputStream outputToClient = clientSocket.getOutputStream();

    //store clientSocket's inputstream into a buffer
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead; //start offset in the data
    byte[] data = new byte[16384]; //create byte array
    //read each byte from InputStream and write it to a ByteArrayOutputStream
    while((nRead = clientSocket.getInputStream().read(data, 0, data.length)) != -1) {
      buffer.write(data, 0, nRead); 
    }
    buffer.flush(); //forces any buffered bytes to be written out
    data = buffer.toByteArray(); //retrieve the underlying byte array 
    System.out.println(new String(data));

    //create buffer reader for clientSocket's inputstream
    InputStream inputStream = new ByteArrayInputStream(data);
    BufferedReader readFromClient = new BufferedReader(new InputStreamReader(inputStream));

    String[] strArr;
    String line;
    String hostname = null;

    //get hostname
    while(  (line=readFromClient.readLine()) != null)
    {
      strArr = line.split(" ");
      if(strArr[0].equals("Host:"))
        hostname = strArr[1];
    }
    System.out.println("Host: " + hostname);

    //create server socket
    Socket serverSocket = new Socket(hostname, 80);
    OutputStream outputToServer = serverSocket.getOutputStream();
    InputStream inputFromServer = serverSocket.getInputStream();

    //forward request to server
    System.out.println("forward request to server...");
    outputToServer.write(data);

    //receive data from server and write response back to client
    byte[] receivedData = new byte[16384];
    int size;
    while((size = inputFromServer.read(receivedData)) != -1)
    {
      System.out.println("write response back to client...");
      System.out.println("size: " + size);
      System.out.println(new String(receivedData));
      outputToClient.write(receivedData, 0, size);
    }

    System.out.println("flushing...");
    outputToClient.flush();
    outputToClient.close();
  }
}

所以基本上我遵循I / O的这种布局:

{
    OutputStream outputToClient = clientSocket.getOutputStream();
    BufferedReader readFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    //step 1
    //get the HTTP request header from your firefox
    String line = readFromClient.readLine();

    //if the Content_Length within the HTTP header exists and not equals to 0
    char [] buffer = new char [#];
    readFromClient.read(buffer, 0, Content_Length); //get the body message

    OutputStream outputToServer = serverSocket.getOutputStream();
    InputStream inputFromServer = serverSocket.getInputStream();
    //step 2
    outputToServer.write(Bytes[])  //forword the HTTP requests to server host.

    //step 3
    byte[] data = new byte[#];
    while ((size = inputFromServer.read(data)) != -1)   //get the response data from server
    {
        //step 4
        outputToClient.write(data, 0, size);    //forward the data to your firefox
    }
}

3 个答案:

答案 0 :(得分:1)

最后一部分存在问题:

byte[] receivedData = new byte[16384];
int size;
while((size = inputFromServer.read(receivedData)) != -1)
{
  System.out.println("write response back to client...");
  System.out.println("size: " + size);
  System.out.println(new String(receivedData));
}
outputToClient.write(receivedData);

所以,假设服务器发送回100万字节。循环通过16384字节的块读取它们,一旦它读取了所有块,它就会在最后一次读取之后发送回receivedData缓冲区中剩余的内容。因此,浏览器只会在服务器发送的1,000,000个字节中收到16384个字节。

您必须将从服务器读取的所有内容发送到浏览器:

byte[] receivedData = new byte[16384];
int size;
while((size = inputFromServer.read(receivedData)) != -1)
{
  System.out.println("write response back to client...");
  System.out.println("size: " + size);
  System.out.println(new String(receivedData));
  outputToClient.write(receivedData, 0, size);      
}

答案 1 :(得分:0)

好的,我发现了问题的原因。我的while循环被阻止,因为我没有检测到HTTP请求的结束。这个答案简洁地解决了我的问题:https://stackoverflow.com/a/9542710

只是在这里张贴那些偶然发现这个问题并迷路的人。

这是我可以使用的更新代码(注意我添加了一个检测HTTP请求结束的方法):

import java.io.*;
import java.net.*;
import java.util.*;

public class proxy2 {

  public static void main(String args[]) throws Exception
  {
    int port = Integer.parseInt(args[0]);
    File fileSub= new File(args[1]); //fileSub
    File fileRediect = new File(args[2]); //fileRedirect

    //create listener socket to listen for requests from browser
    ServerSocket listener = new ServerSocket(port);

    while (true) {
      proxy_func(listener.accept());
    }

  }

  public static void proxy_func(Socket clientSocket) throws Exception{

    OutputStream outputToClient = clientSocket.getOutputStream();
    InputStream inputFromClient = clientSocket.getInputStream();

    //store clientSocket's inputstream into a buffer

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead; //start offset in the data
    byte[] data = new byte[16384]; 
    //read each byte from InputStream and write it to a ByteArrayOutputStream
    while((nRead = inputFromClient.read(data, 0, data.length)) != -1) {
      System.out.println("reading from client...");
      buffer.write(data, 0, nRead);
      //if reached end of HTTP request, break out of this loop
      if(endOfRequest(data) == true)
        break;
    }
    buffer.flush(); //forces any buffered bytes to be written out
    data = buffer.toByteArray(); //retrieve the underlying byte array 
    System.out.println(new String(data));

    //create buffered reader for byte array input stream (request data)
    InputStream inputStream = new ByteArrayInputStream(data);
    BufferedReader readFromClient = new BufferedReader(new InputStreamReader(inputStream));

    String[] strArr;
    String line;
    String hostname = null;

    //get hostname
    while(  (line=readFromClient.readLine()) != null)
    {
      strArr = line.split(" ");
      if(strArr[0].equals("Host:"))
        hostname = strArr[1];
    }
    System.out.println("Host: " + hostname);

    //create server socket
    Socket serverSocket = new Socket(hostname, 80);
    OutputStream outputToServer = serverSocket.getOutputStream();
    //InputStream inputFromServer = serverSocket.getInputStream();

    //forward request to server
    System.out.println("forward request to server...");
    outputToServer.write(data);

    //receive data from server and write response back to client
    DataInputStream inputFromServer = new DataInputStream(serverSocket.getInputStream());
    byte[] receivedData = new byte[1024];
    int size;
    while((size = inputFromServer.read(receivedData, 0, receivedData.length)) != -1)
    {
      System.out.println("write response back to client...");
      System.out.println(new String(receivedData));
      outputToClient.write(receivedData, 0, size);
      outputToClient.flush();
    }

    System.out.println("done sending");
    outputToClient.close();
  }

  //method to detect end of HTTP request
  //return true if detected, otherwise false
  public static boolean endOfRequest(byte[] data) throws Exception
  {
    System.out.println("parsing request..");
    BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(data)));
    String line;

    while( (line=br.readLine()) != null) {
      if (line.equals(""))
        return true;
    }
    return false;
  }
}

答案 2 :(得分:0)

你这样做是错的。 HTTP代理只需要读取CONNECT命令并对其进行操作。之后,您只需要尽快同时复制两个方向的字节。您尝试理解代理中的HTTP协议越多,您将引入的错误就越多。