我的编程任务要求我在客户端和服务器之间创建一个代理。我的客户端通过代理向服务器发出请求,然后代理转发它并将服务器的响应返回给客户端。
这是我采取的步骤:
1)从客户端获取请求并将请求数据存储到字节数组中
2) 使用缓冲读取器从字节数组中读取
3) 从 Host: 标头字段中获取主机名并用它创建一个 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
}
}