【问题标题】:Cannot process url with vertical/pipe bar in Java/Apache HttpClient无法在 Java/Apache HttpClient 中处理带有垂直/竖线的 url
【发布时间】:2013-08-21 09:27:22
【问题描述】:

如果我想处理这个 url 例如:

post = new HttpPost("http://testurl.com/lists/lprocess?action=LoadList|401814|1");

Java/Apache 不允许,因为它说竖线 ("|") 是非法的。

用双斜杠转义也不行:

post = new HttpPost("http://testurl.com/lists/lprocess?action=LoadList\\|401814\\|1");

^ 也不好用。

对如何使这项工作有任何建议?

【问题讨论】:

    标签: java apache apache-httpclient-4.x apache-commons-httpclient


    【解决方案1】:

    试试URLEncoder.encode()

    注意:你应该编码action=之后的字符串而不是complete URL

    post = new HttpPost("http://testurl.com/lists/lprocess?action="+URLEncoder.encode("LoadList|401814|1","UTF-8"));
    

    参考http://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html

    【讨论】:

    • 这是正确的。对整个字符串进行编码将失败,因为 URI 无法识别编码的 http://
    【解决方案2】:

    您必须将 URL 中的 | 编码为 %7C

    考虑使用 HttpClient 的 URIBuilder,它会为您处理转义,例如:

    final URIBuilder builder = new URIBuilder();
    builder.setScheme("http")
        .setHost("testurl.com")
        .setPath("/lists/lprocess")
        .addParameter("action", "LoadList|401814|1");
    final URI uri = builder.build();
    final HttpPost post = new HttpPost(uri);
    

    【讨论】:

    • 这个答案比the one by Tarsem 更详细,但有助于了解 URI 中有哪些部分。另一方面,它将处理和编码很好地隐藏在URIBuilder的实现细节中。
    【解决方案3】:

    我有同样的问题,我解决了它替换 |对于它的编码值 => %7C 和 ir 工作

    从这里

    post = new HttpPost("http://testurl.com/lists/lprocess?action=LoadList|401814|1");
    

    到这里

    post = new HttpPost("http://testurl.com/lists/lprocess?action=LoadList\\%7C401814\\%7C1");
    

    【讨论】:

      【解决方案4】:

      您可以使用URLEncoder:对 URL 参数进行编码:

      post = new HttpPost("http://testurl.com/lists/lprocess?action=" + URLEncoder.encode("LoadList|401814|1", "UTF-8"));
      

      这将为您编码所有特殊字符,而不仅仅是管道。

      【讨论】:

        【解决方案5】:

        在帖子中,我们不会将参数附加到 url。下面的代码添加并 urlEncodes 您的参数。取自:http://hc.apache.org/httpcomponents-client-ga/quickstart.html

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://testurl.com/lists/lprocess");
        
            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("action", "LoadList|401814|1"));
            httpPost.setEntity(new UrlEncodedFormEntity(nvps));
            HttpResponse response2 = httpclient.execute(httpPost);
        
            try {
                System.out.println(response2.getStatusLine());
                HttpEntity entity2 = response2.getEntity();
                // do something useful with the response body
                // and ensure it is fully consumed
        
                String response = new Scanner(entity2.getContent()).useDelimiter("\\A").next();
                System.out.println(response);
        
        
                EntityUtils.consume(entity2);
            } finally {
                httpPost.releaseConnection();
            }
        

        【讨论】:

        • 引用:“在帖子中,我们不会将参数附加到 url。” 这不是真的。您可以使用 URL 参数在 POST 请求中很好地发送数据,并且不会与任何标准冲突。
        猜你喜欢
        • 1970-01-01
        • 2015-07-26
        • 1970-01-01
        • 2015-11-26
        • 1970-01-01
        • 1970-01-01
        • 2012-10-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多