11

たとえば、この 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");

^ それもうまくいきません。

これを機能させる方法はありますか?

4

5 に答える 5

11

試してみてくださいURLEncoder.encode()

注:action= notの後にある文字列をエンコードする必要があります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

于 2013-08-19T14:31:23.883 に答える
9

|URL を としてエンコードする必要があります%7C

URIBuilderエスケープを処理するHttpClient の使用を検討してください。

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);
于 2013-08-19T14:26:36.770 に答える
0

URLEncoderを使用して URL パラメータをエンコードできます。

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

これにより、パイプだけでなく、すべての特殊文字がエンコードされます。

于 2013-08-19T14:30:41.020 に答える
0

投稿では、パラメーターを 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();
    }
于 2013-08-19T14:56:36.893 に答える