gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:02:50 25 4
gpt4 key购买 nike

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

这是我采取的步骤:

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
}
}

最佳答案

最后一部分有问题:

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 字节的 block 读取它们,一旦读取了所有 block ,它就会发回上次读取后 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);
}

关于java - Java 中的代理 - 客户端不显示响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19207599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com