doubi4617 2011-11-11 22:18
浏览 88
已采纳

Java阅读POST,奇怪的十六进制解释

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?

  • 写回答

2条回答 默认 最新

  • dongshi3061 2011-11-11 23:01
    关注

    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.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)