Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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);

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");

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.

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();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top