【问题标题】:How do I cater for empty strings while encoding parameters for a GET request?在为 GET 请求编码参数时如何处理空字符串?
【发布时间】:2012-08-17 20:05:59
【问题描述】:

我正在开发一个使用 HTTP GET 请求与服务器通信的 J2ME 应用程序。我已经有了生成 URL 编码参数的方法。

在目前的形式中,它不能处理空字符串,我见过的其他代码 sn-ps 也有这个缺陷,因为它们都依赖于比较字符串参数的各个字符.I have previously asked a question related to this empty char dilemma

编辑:

对服务器的请求(Play 1.0)的格式为

http://server.com/setName/firstname/othername/lastname

参数不能为空,所以 http:server.com/setname/firstname//lastname 无效

参数是从 json 对象中检索的。目前我拥有的 url 编码方法将正确编码所有提取的参数,并保留任何无法按原样转换的字符。字符串中的空格,如“Jo e”和空格字符本身将分别编码为 Jo%20e 和 %20。 JSON 对象

{ "firstname":"joe"
  "othername":""
  "lastname":"bloggs"
}  

但是会导致无效的 url http://server.com/setname/joe//bloggs,因为 othername 参数是一个空字符串,并且由我的方法保留。

我可以检查要返回的字符串是否为空并返回一个空格字符。但是我想知道是否对这种方法没有更好的修改,或者没有一种更强大的全新方法?

 public static String urlEncode(String s) {
    ByteArrayOutputStream bOut = null;
    DataOutputStream dOut = null;
    ByteArrayInputStream bIn = null;
    StringBuffer ret = new StringBuffer();
    try {
        bOut=new ByteArrayOutputStream();
        dOut = new DataOutputStream(bOut);
        //return value
        dOut.writeUTF(s);
        bIn = new ByteArrayInputStream(bOut.toByteArray());
        bIn.read();
        bIn.read();
        int c = bIn.read();
        while (c >= 0) {
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '*' || c == '_') {
                ret.append((char) c);
            } else if (c == ' ' ) {
                //ret.append('+');
                ret.append("%20");
            } else {
                if (c < 128) {
                    appendHex(c, ret);
                } else if (c < 224) {
                    appendHex(c, ret);
                    appendHex(bIn.read(), ret);
                } else if (c < 240) {
                    appendHex(c, ret);
                    appendHex(bIn.read(), ret);
                    appendHex(bIn.read(), ret);
                }
            }
            c = bIn.read();
        }
    } catch (IOException ioe) {
        System.out.println("urlEncode:"+ioe);
        return s;
    }
       return ret.toString();
}

private static void appendHex(int arg0, StringBuffer buff) {
    buff.append('%');
    if (arg0 < 16) {
        buff.append('0');
    }
    buff.append(Integer.toHexString(arg0));
}

【问题讨论】:

  • “但是它不能/不能满足空字符串” - 究竟是什么意思?空字符串究竟在哪里引起了问题,那是什么问题?是否有异常/崩溃,或者您只是得到意外/不希望的行为?
  • 所有参数都需要吗?是否有一个可选参数,或者可以有多个?

标签: string java-me char urlencode


【解决方案1】:

根据RFC 1738,没有空字符的URL编码。这让您有四个选择。

  1. 要求填写所有字段。这可能不是一个好的选择,具体取决于您的应用程序的用途,因为用户可能没有特定字段的数据,或者不想共享它。

  2. 如果只有一个字段可以为空,请重新排序 URL 参数,使其位于最后。这将导致/joe/bloggs/ 而不是/joe//bloggs

  3. 如果一个 URL 可能有多个空参数,最好使用查询字符串。这看起来像/setName?firstname=joe&amp;othername=&amp;lastname=bloggs。可以省略或包含未使用的参数。

  4. 使用 POST 而不是 GET。只需使用 url /setName 并将所有字段放入表单中。根据 URL,显然正在执行的操作似乎更适合 POST 而不是 GET。

【讨论】:

  • 我可能应该补充一点,我只在客户端工作,所以我不能随意进行建议的更改。这就是为什么我的重点是能够检测空字符串并替换它们带有空格等合法字符
  • 服务器端是在开发中,还是已经建好?
  • 服务器端已经建好,向其他开发者公开
  • 然后您应该与服务器端的开发人员/文档联系,看看应该如何处理这种情况。如果没有明确的解释,并且没有指定所有字段都是必填字段,请提交一份错误报告,说明如果未提供非必填字段会破坏 URL。
猜你喜欢
  • 2013-09-08
  • 1970-01-01
  • 1970-01-01
  • 2015-06-24
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
相关资源
最近更新 更多