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?
I hope these examples of creating Java REST (RESTful) clients using the Apache HttpClient have been helpful. As mentioned, these examples are heavily based on the Apache HttpClient samples, and I recommend looking at that code for more examples.
Both can be used along with popular HTTP clients such as Apache HttpClient, OkHttp, and the old HttpURLConnection. However, we can't plug Java HttpClient into these two interfaces. It's rather seen as an alternative to them. We can use Java HttpClient to make synchronous and asynchronous requests, convert requests and responses, add timeouts, etc.
In the examples, we use httpbin.org, which is a freely available HTTP request and response service, and the webcode.me, which is a tiny HTML page for testing. Java 11 introduced HttpClient library. Before Java 11, developers had to use rudimentary URLConnection, or use third-party library such as Apache HttpClient, or OkHttp.
We build a HEAD request. We use HttpRequest.BodyPublishers.noBody () since we do not have any body in the request. We send a request and receive a body. We get the headers from the response and print them to the console. This is a sample output. The HTTP GET method requests a representation of the specified resource.
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
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With