先决条件
<artifactId>camel-http</artifactId>
)问题
目前我使用以下代码将POST-Parameters设置为邮件正文。 camel HTTP-Component读取参数并发送它。
.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")
问题在于某些参数本身就是URL。 所以像这样的东西应该作为POST-Request发送:
postparameter1=a&postparameter2=http://www.`...`.com?urlparam1=value1&urlparam2=value2&postparameter3=b
我的问题是如何发送“http://www。...
。com?urlparam1 = value1&amp; urlparam2 = value2”作为postparameter2的值。
提前致谢。
此致
最高
答案 0 :(得分:1)
如上所述,以下内容适用于我。 这个想法是解析一个给定的url拳头并在之后再次编码。 这避免了双重编码。
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;
}
答案 1 :(得分:0)
我在这里提到了如何通过camel HTTP发送字符串消息,我希望这对你有所帮助。另一个是我们需要添加基本的休息认证。用户名和密码是你的应用程序休息认证凭证。
from("seda:httpSender")
.log("Inside Http sender")
.process(new Processor(){
@Override
public void process(Exchange exchange) throws Exception {
// Camel will populate all request.parameter and request.headers,
// no need for placeholders in the "from" endpoint
String content = exchange.getIn().getBody(String.class);
System.out.println("Outbound message string : "+content);
// This URI will override http://dummyhost
exchange.getIn().setHeader(Exchange.HTTP_URI, "http://localhost:9090/httpTest");
// Add input path. This will override the original input path.
// If you need to keep the original input path, then add the id to the
// URI above instead
// exchange.getIn().setHeader(Exchange.HTTP_PATH, id);
// Add query parameter such as "?name=xxx"
exchange.getIn().setHeader(Exchange.HTTP_QUERY, "outboundMessage="+content);
exchange.getIn().setHeader(Exchange.HTTP_METHOD, "POST");
}
})
.doTry()
.log("Message added as a parameter")
.to("http4://localhost:9090/httpTest?authMethod=Basic&authPassword=admin&authUsername=admin")
.log("HTTP message transfer success")
.doCatch(Exception.class)
.log("HTTP message transfer failed")
.end();