Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 318
heisenbergman Avatar asked Aug 19 '13 14:08

heisenbergman


People also ask

Is it possible to create RESTful client using Apache httpclient in Java?

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.

Can we use Java httpclient with httpurlconnection and httpurlconnection?

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.

What is httpclient in Java 11?

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.

What is the difference between HTTP HEAD and HTTP GET?

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.


2 Answers

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

like image 67
Tarsem Singh Avatar answered Sep 30 '22 21:09

Tarsem Singh


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");
like image 45
Ivor Avatar answered Sep 30 '22 20:09

Ivor