Java读取POST请求,奇怪的十六进制解释

huangapple go评论96阅读模式
英文:

Java reading POST, weird hex interpretation

问题

我正在尝试让我的Go服务器向我的Java应用程序发送一个字节数组的POST请求。

原始数据字节如下(添加了换行符):

FABFB5DA
76657273696F6E000000000055000000
67C4256E409C0000010000000000000013A3BD4E
00000000010000000000000000000000000000000000
FFFF6D7B74F5479D010000000000000000000000000000000000
FFFF51DB4826479D59128BC986A833460001000000

我在Java中接收到的数据如下:

2D9F8B52044
76657273696F6E000000000055000000
C856CDEB40FA0000010000000000000009A33A94E
00000000010000000000000000000000000000000000
2C72C76D7B7413147F9010000000000000000000000000000000000
2C72C75120AC482647F920AB4678B620194D4E0001000000

我尝试将数据发送到Go应用程序的另一个函数,它可以正常工作。

作为我的Java应用程序的基础,我使用了来自http://fragments.turtlemeat.com/javawebserver.php的代码。

它被修改为处理POST请求。在使用input.readLine()清除HTTP请求的前几行之后,我使用以下代码读取数据:

int hex=0;
while((hex=input.read())>-1){
System.out.printf("%02X", hex);
}

Wireshark的十六进制转储如下:

0000   50 4f 53 54 20 2f 68 61 6e 64 6c 65 70 6f 73 74  POST /handlepost
0010   20 48 54 54 50 2f 31 2e 31 0d 0a 43 6f 6e 74 65   HTTP/1.1..Conte
0020   6e 74 2d 54 79 70 65 3a 20 0d 0a 48 6f 73 74 3a  nt-Type: ..Host:
0030   20 6c 6f 63 61 6c 68 6f 73 74 3a 38 30 38 30 0d   localhost:8080.
0040   0a 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a  .Content-Length:
0050   20 31 30 39 0d 0a 41 63 63 65 70 74 2d 45 6e 63   109..Accept-Enc
0060   6f 64 69 6e 67 3a 20 67 7a 69 70 0d 0a 55 73 65  oding: gzip..Use
0070   72 2d 41 67 65 6e 74 3a 20 41 70 70 45 6e 67 69  r-Agent: AppEngi
0080   6e 65 2d 47 6f 6f 67 6c 65 3b 20 28 2b 68 74 74  ne-Google; (+htt
0090   70 3a 2f 2f 63 6f 64 65 2e 67 6f 6f 67 6c 65 2e  p://code.google.
00a0   63 6f 6d 2f 61 70 70 65 6e 67 69 6e 65 29 0d 0a  com/appengine)..
00b0   0d 0a fa bf b5 da 76 65 72 73 69 6f 6e 00 00 00  ......version...
00c0   00 00 55 00 00 00 e9 56 ea 91 40 9c 00 00 01 00  ..U....V..@.....
00d0   00 00 00 00 00 00 09 a3 bd 4e 00 00 00 00 01 00  .........N......
00e0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00f0   ff ff 6d 7b 74 f5 47 9d 01 00 00 00 00 00 00 00  ..m{t.G.........
0100   00 00 00 00 00 00 00 00 00 00 ff ff 51 db 48 26  ............Q.H&
0110   47 9d 20 c7 46 78 a6 d5 4d 4e 00 01 00 00 00     G. .Fx..MN.....

我应该如何正确读取这些字节?

英文:

I`m trying to make my Go server send POST request with an array of bytes to my Java application.

The original data bytes look like (with added line brakes):

FABFB5DA
76657273696F6E000000000055000000
67C4256E409C0000010000000000000013A3BD4E
00000000010000000000000000000000000000000000
FFFF6D7B74F5479D010000000000000000000000000000000000
FFFF51DB4826479D59128BC986A833460001000000

What I'm receiving in Java looks like:

2D9F8B52044
76657273696F6E000000000055000000
C856CDEB40FA0000010000000000000009A33A94E
00000000010000000000000000000000000000000000
2C72C76D7B7413147F9010000000000000000000000000000000000
2C72C75120AC482647F920AB4678B620194D4E0001000000

I tried posting the data to another function of my Go application and it works fine.

As the base for my Java application, I used the code from
http://fragments.turtlemeat.com/javawebserver.php

It is altered to handle POST requests. After clearing out the first couple lines of HTTP request with input.readLine(), I read the data with:

int hex=0;
while((hex=input.read())>-1){
System.out.printf("%02X", hex);
}

The hex dump from Wireshark looks like:

0000   50 4f 53 54 20 2f 68 61 6e 64 6c 65 70 6f 73 74  POST /handlepost
0010   20 48 54 54 50 2f 31 2e 31 0d 0a 43 6f 6e 74 65   HTTP/1.1..Conte
0020   6e 74 2d 54 79 70 65 3a 20 0d 0a 48 6f 73 74 3a  nt-Type: ..Host:
0030   20 6c 6f 63 61 6c 68 6f 73 74 3a 38 30 38 30 0d   localhost:8080.
0040   0a 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a  .Content-Length:
0050   20 31 30 39 0d 0a 41 63 63 65 70 74 2d 45 6e 63   109..Accept-Enc
0060   6f 64 69 6e 67 3a 20 67 7a 69 70 0d 0a 55 73 65  oding: gzip..Use
0070   72 2d 41 67 65 6e 74 3a 20 41 70 70 45 6e 67 69  r-Agent: AppEngi
0080   6e 65 2d 47 6f 6f 67 6c 65 3b 20 28 2b 68 74 74  ne-Google; (+htt
0090   70 3a 2f 2f 63 6f 64 65 2e 67 6f 6f 67 6c 65 2e  p://code.google.
00a0   63 6f 6d 2f 61 70 70 65 6e 67 69 6e 65 29 0d 0a  com/appengine)..
00b0   0d 0a fa bf b5 da 76 65 72 73 69 6f 6e 00 00 00  ......version...
00c0   00 00 55 00 00 00 e9 56 ea 91 40 9c 00 00 01 00  ..U....V..@.....
00d0   00 00 00 00 00 00 09 a3 bd 4e 00 00 00 00 01 00  .........N......
00e0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00f0   ff ff 6d 7b 74 f5 47 9d 01 00 00 00 00 00 00 00  ..m{t.G.........
0100   00 00 00 00 00 00 00 00 00 00 ff ff 51 db 48 26  ............Q.H&
0110   47 9d 20 c7 46 78 a6 d5 4d 4e 00 01 00 00 00     G. .Fx..MN.....

How should I go about properly reading those bytes?

答案1

得分: 4

问题 - 或者一个问题 - 在这一部分中:

    BufferedReader input =
        new BufferedReader(new InputStreamReader(connectionsocket.
        getInputStream()));

InputStreamReader必须将字节转换为字符,而你没有告诉它如何做到这一点 - 或者换句话说,你隐式地告诉它使用你的平台的默认字符集。对于ASCII范围内的字节(007F),这似乎工作得很好,但对于超出该范围的字节则会出错。

你的应用程序似乎正在发送二进制数据(我指的是非文本数据);你应该将其改为以文本形式编码所有内容,或者不使用InputStreamReader。(不幸的是,这使得处理头部行变得有点棘手,因为你不能再使用BufferedReader.readLine()。)

英文:

The problem — or a problem — is in this bit:

    BufferedReader input =
        new BufferedReader(new InputStreamReader(connectionsocket.
        getInputStream()));

The InputStreamReader has to translate from bytes to characters, and you're not telling it how to do that — or, put another way, you're implicitly telling it to use your platform's default character-set. That seems to be working fine for bytes in the ASCII range (00 to 7F), but breaking on bytes outside that range.

Your application seems to be sending binary data (by which I mean non-textual data); you should either change it to encode everything in a textual form, or else not use InputStreamReader. (Unfortunately, this makes it a bit trickier to handle the header lines, since you can't use BufferedReader.readLine() anymore.)

答案2

得分: 0

你是如何获取输入流的?如果你必须编写代码来跳过头部,那么我猜你可能没有使用HttpURLConnection或其他库。在这种情况下,你可能正在查看分块传输编码的额外部分。

https://en.wikipedia.org/wiki/Chunked_transfer_encoding

英文:

How are you getting your input stream? If you have to write code to skip the headers then I would guess that you're not using HttpURLConnection or some other library. In which case you might be looking at the extra bits from a chunked transfer encoding.

https://en.wikipedia.org/wiki/Chunked_transfer_encoding

huangapple
  • 本文由 发表于 2011年11月12日 06:18:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/8100663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定