【问题标题】:Camel HTTP Endpoint: How to set URL-String to POST ParameterCamel HTTP Endpoint:如何将 URL-String 设置为 POST 参数
【发布时间】:2015-04-30 16:08:12
【问题描述】:

先决条件

  • Apache Tomcat 7
  • 春季 3.2.11.RELEASE
  • Apache Camel 2.14.1
  • Camel HTTP 端点 (<artifactId>camel-http</artifactId>)

问题

目前我使用以下代码将 POST-Parameters 设置为消息正文。 骆驼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&parameter2=b")

问题在于某些参数本身就是 URL。 所以像这样的东西应该作为 POST-Request 发送:

postparameter1=a&postparameter2=http://www.`...`.com?urlparam1=value1&urlparam2=value2&postparameter3=b

我的问题是如何发送“http://www.....com?urlparam1=value1&urlparam2=value2”作为 postparameter2 的值。

提前致谢。

问候,

最大

【问题讨论】:

标签: java http http-post apache-camel


【解决方案1】:

正如上面提到的 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;
}

【讨论】:

    【解决方案2】:

    我在这里提到了如何通过camel HTTP post发送字符串消息,希望对您有所帮助。另一个是我们需要添加基本的rest身份验证。用户名和密码是您的应用程序rest身份验证凭据。

    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();
    

    【讨论】:

      猜你喜欢
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 2012-08-16
      • 1970-01-01
      • 2017-01-31
      • 1970-01-01
      相关资源
      最近更新 更多