2015-04-30 18 views
2

先決條件駱駝HTTP端點:如何設置URL字符串到之後的參數

  • 的Apache Tomcat 7
  • 春3.2.11.RELEASE
  • Apache的駱駝2.14.1
  • 駱駝HTTP端點(<artifactId>camel-http</artifactId>

問題

目前我使用下面的代碼來設置郵件正文的POST參數。 駱駝HTTP組件讀取參數併發送它。

.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請求:

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

我的問題是如何發送 「http://www... .COM urlparam1 =值& urlparam2 =值2?」 作爲postparameter2的價值。

在此先感謝。

問候,

最大

+1

您是否嘗試過使用['java.net.URLEncode.encode'(http://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html#encode( java.lang.String中,%20java.lang.String))? –

回答

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; 
} 
0

我這裏如何通過HTTP駱駝寄往字符串消息中提到,我希望這將有助於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();