Camel HTTP 端点:如何将 URL-String 设置为 POST 参数
Camel HTTP Endpoint: How to set URL-String to POST Parameter
先决条件
- 阿帕奇 Tomcat 7
- Spring 3.2.11.RELEASE
- 阿帕奇骆驼 2.14.1
- Camel HTTP 端点(
<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&urlparam2=value2”作为 postparameter2 的值。
提前致谢。
此致,
最大
正如上面提到的isim,以下对我有用。
这个想法是解析给定的 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;
}
我在这里提到了如何通过 camel HTTP post 发送字符串消息,我希望这对 you.Another 有帮助,一个是我们需要添加基本的休息 authentication.Username 密码是你的应用程序休息身份验证凭据。
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();
先决条件
- 阿帕奇 Tomcat 7
- Spring 3.2.11.RELEASE
- 阿帕奇骆驼 2.14.1
- Camel HTTP 端点(
<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&urlparam2=value2”作为 postparameter2 的值。
提前致谢。
此致,
最大
正如上面提到的isim,以下对我有用。 这个想法是解析给定的 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;
}
我在这里提到了如何通过 camel HTTP post 发送字符串消息,我希望这对 you.Another 有帮助,一个是我们需要添加基本的休息 authentication.Username 密码是你的应用程序休息身份验证凭据。
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();