简体   繁体   English

在Play Framework中获取二进制HTTP GET参数

[英]Getting binary HTTP GET parameter in Play Framework

I need to get binary data in GET request by Play Framework. 我需要在Play Framework的GET请求中获取二进制数据。 This is used to get info_hash from BitTorrent clients. 这用于从BitTorrent客户端获取info_hash。

I get it like this: 我这样得到:

byte[] infoHash = params.get("info_hash").getBytes("ISO-8859-1")

Unfortunately all non-ascii symbols replaced by 0x3f. 不幸的是,所有非ascii符号都被0x3f取代。

PS I can get url encoded parameters from Http.Request.current().querystring, but this is a bad idea. PS我可以从Http.Request.current()。querystring获取url编码参数,但这是一个坏主意。

Update: I override play.data.parsing.UrlEncodedParser.parse(InputStream is) method with my variant where used ISO-8859-1 in parameter instead of hardcoded UTF-8 as in original and all is working as it should. 更新:我用我的变量覆盖了play.data.parsing.UrlEncodedParser.parse(InputStream is)方法,其中在参数中使用ISO-8859-1而不是原始的硬编码UTF-8,并且一切正常。 But i still looking for a better way, because i don't want to edit framework sources. 但我仍然在寻找更好的方法,因为我不想编辑框架资源。

According to http://wiki.theory.org/BitTorrent_Tracker_Protocol : 根据http://wiki.theory.org/BitTorrent_Tracker_Protocol

info_hash info_hash

The 20 byte sha1 hash of the bencoded form of the info value from the metainfo file. 来自metainfo文件的信息值的bencoded形式的20字节sha1哈希。

A SHA1 sum looks like this: 92a11182a8405cbd8d25cd3cc3334fc6155bec06 SHA1总和如下所示: 92a11182a8405cbd8d25cd3cc3334fc6155bec06

Each successive pair of bytes in the representation of a byte. 字节表示中的每个连续字节对。 Although this representation itself might be encoded, it's not a URL-encoding of the bytes for the info_hash. 虽然这种表示本身可能是编码的,但它不是 info_hash字节的URL编码。

So you need to convert each pair of chars from the String into a byte. 因此,您需要将每对字符串从字符串转换为字节。 If you find a library that does it instead, stick to it. 如果你找到一个代替它的库,坚持下去。 If not, feel free to use this code: 如果没有,请随意使用此代码:

byte[] decode(String enc) {
    if (enc.length() % 2 != 0) throw new NumberFormatException();

    byte arr[] = new byte[enc.length() / 2];
    int c = 0;
    for (int i = 0; i < enc.length(); i += 2) {
        arr[c++] = Integer.valueOf(enc.substring(i, i + 2), 16).byteValue();
    }
    return arr;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM