Prerequisites
<artifactId>camel-http</artifactId>)Problem
At the moment I use following code to set POST-Parameters to the message body. The camel HTTP-Component reads the parameters and sends it.
.setHeader(Exchange.HTTP_METHOD, constant(HttpMethods.POST.name()))
.setHeader(Exchange.CONTENT_TYPE, constant("application/x-www-form-urlencoded; charset: UTF-8"))
.setHeader(Exchange.CONTENT_ENCODING, constant("UTF-8"))
.setBody("parameter1=a¶meter2=b")
The problem ist that some parameters are URLs itself. So something like this should be send as POST-Request:
postparameter1=a&postparameter2=http://www.`...`.com?urlparam1=value1&urlparam2=value2&postparameter3=b
My question is how to send "http://www.....com?urlparam1=value1&urlparam2=value2" as value of postparameter2.
Thanks in advance.
Regards,
Max
As isim mentioned above, following works for me. The idea is to parse a given url fist and to encode it again afterwards. This avoids double encoding.
import java.io.UnsupportedEncodingException;
import java.net.*;
public static String getEncodedURL(String urlString) {
final String encodedURL;
try {
String decodedURL = URLDecoder.decode(urlString, "UTF-8");
URL url = new URL(decodedURL);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
final URL urlFromDecoding = uri.toURL();
encodedURL = URLEncoder.encode(urlFromDecoding.toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
...
} catch (MalformedURLException e) {
...
} catch (URISyntaxException e) {
...
}
return encodedURL;
}
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