Cannot process url with vertical/pipe bar in Java/Apache HttpClient
Asked Answered
P

5

12

If I want to process this url for example:

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

Java/Apache won't let me because it says that the vertical bar ("|") is illegal.

escaping it with double slashes doesn't work as well:

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

^ that doesn't work as well.

Any suggestions how to make this work?

Pindaric answered 19/8, 2013 at 14:24 Comment(0)
F
11

try with URLEncoder.encode()

Note: you should encode string which is after action= not complete URL

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

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

Flagwaving answered 19/8, 2013 at 14:31 Comment(1)
This is correct. Encoding the whole String will fail because URI would not recognize the encoded http://.Hendiadys
F
10

You must encode | in a URL as %7C.

Consider using HttpClient's URIBuilder which takes care of the escaping for you, e.g.:

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);
Farci answered 19/8, 2013 at 14:26 Comment(1)
This answer is more verbose than the one by Tarsem but helps realize what parts there are in a URI. On the other hand, it keeps the handling and encoding nicely hidden in the implementation details of URIBuilder.Sacramental
P
1

I had same problem and I solve it replacing the | for a encoded value of it => %7C and ir works

From this

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

To this

post = new HttpPost("http://testurl.com/lists/lprocess?action=LoadList\\%7C401814\\%7C1");
Parthenon answered 19/5, 2017 at 9:10 Comment(0)
S
0

You can encode URL parameters using the URLEncoder:

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

This will encode all special characters, not just the pipe, for you.

Soutine answered 19/8, 2013 at 14:30 Comment(0)
I
0

In post we don't attach parameters to url. Code below adds and urlEncodes your parameter. It's taken from: 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();
    }
Inverness answered 19/8, 2013 at 14:56 Comment(1)
Quoting: "In post we don't attach parameters to url." This is not true. You can very well send data in a POST request using URL parameters and it does not collide with any standards.Sacramental

© 2022 - 2024 — McMap. All rights reserved.