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?

heisenbergman
  • 1,459
  • 4
  • 16
  • 33

5 Answers5

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

Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
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);
Jonathan
  • 20,053
  • 6
  • 63
  • 70
  • This answer is more verbose than [the one by Tarsem](http://stackoverflow.com/a/18316373/177710) 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`. – Oliver Jan 07 '15 at 12:03
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");
Ivor
  • 578
  • 1
  • 11
  • 27
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.

Michelle
  • 2,830
  • 26
  • 33
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();
    }
Shyam Bhimani
  • 1,310
  • 1
  • 22
  • 37
greenmarker
  • 1,599
  • 1
  • 21
  • 29
  • 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. – Oliver Jan 07 '15 at 12:09