java中转发http包

发布于 2025-01-04 13:06:40 字数 1026 浏览 1 评论 0原文

我正在尝试用 java 编写一个 HTTP 代理服务器。我的应用程序从浏览器获取 GET 请求并将其转发到目的地。我想读取响应包的标头,然后将其转发回浏览器。这对我来说非常适合 text/html-content,只要它没有用 gzip 编码。我尝试了多种方法来执行此操作,目前正在使用 DataInputStreamDataOutputStream 但浏览器仅显示奇怪的符号。

这是代码的简化版本:

ArrayList<String> headerlist = new ArrayList<String>();
InputStream input = clientsocket.getInputStream();
dis = new DataInputStream(input);
serverinputstream = new InputStreamReader(input);
bufferreader = new BufferedReader(serverinputstream);

while(!(line = bufferedreader.readLine()).equals("")) {          
      headerlist.add(line);
    }

PrintWriter pw = new PrintWriter(serveroutputstream, false);
DataOutputStream out = new DataOutputStream(serveroutputstream);

for (int i = 0; i < headerlist.size(); i++) {
    pw.println(headerlist.get(i));  
}       
pw.println();

int bit;
while((bit = dis.read()) != -1) {
    out.writeByte(bit);
}
out.flush();
dis.close();
out.close();

此代码仅处理非纯文本的数据,但它似乎不起作用。我应该使用其他方法还是我只是做错了什么?

I'm trying to write a HTTP proxy-server in java. My application takes a GET request from a browser and forwards it to its destination. I would like to read the headers of response package and then forward it back to the browser. This works great for me with text/html-content aslong as its not encoded in gzip. I've tried multiple ways to do this and I'm currently using a DataInputStream and a DataOutputStream but the browser only shows weird symbols.

Here is a simplified version of the code:

ArrayList<String> headerlist = new ArrayList<String>();
InputStream input = clientsocket.getInputStream();
dis = new DataInputStream(input);
serverinputstream = new InputStreamReader(input);
bufferreader = new BufferedReader(serverinputstream);

while(!(line = bufferedreader.readLine()).equals("")) {          
      headerlist.add(line);
    }

PrintWriter pw = new PrintWriter(serveroutputstream, false);
DataOutputStream out = new DataOutputStream(serveroutputstream);

for (int i = 0; i < headerlist.size(); i++) {
    pw.println(headerlist.get(i));  
}       
pw.println();

int bit;
while((bit = dis.read()) != -1) {
    out.writeByte(bit);
}
out.flush();
dis.close();
out.close();

This code only handles data that isnt plain text but it doesnt seem to be working. Should I use another method or I am just doing something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停滞 2025-01-11 13:06:40

我认为你可能把事情过于复杂化了。您的代理只是将请求转发到另一个目的地。它没有理由关心它是转发文本还是二进制数据。应该没有什么区别。

也没有理由单独读取和写入标头。您需要做的就是将整个请求正文复制到新的输出流。

怎么样:

//...
InputStream input = clientsocket.getInputStream();
streamCopy(input, serveroutputstream);

//...


public void streamCopy(InputStream in, OutputStream out) throws IOException {
    int read = 0;
    byte[] buffer = new byte[4096];
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    } 
}

I think you may be overcomplicating things a bit. Your proxy is just forwarding a request on to another destination. There's no reason for it to care about whether it is forwarding text or binary data. It should make no difference.

There's also no reason to read and write the headers individually. All you should need to do is copy the entire request body to the new output-stream.

What about something like:

//...
InputStream input = clientsocket.getInputStream();
streamCopy(input, serveroutputstream);

//...


public void streamCopy(InputStream in, OutputStream out) throws IOException {
    int read = 0;
    byte[] buffer = new byte[4096];
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文