Search code examples
javaapacheapache-httpclient-4.xapache-commons-httpclient

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


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?


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