Java 驼峰HTTP端点:如何将URL字符串设置为POST参数

Java 驼峰HTTP端点:如何将URL字符串设置为POST参数,java,http,http-post,apache-camel,Java,Http,Http Post,Apache Camel,先决条件 ApacheTomcat7 弹簧3.2.11.释放 ApacheCamel 2.14.1 驼峰HTTP端点(...com?urlparam1=value1&urlparam2=value2)作为后参数2的值 提前谢谢 问候, Max正如上面提到的isim,以下内容对我很有用。 其思想是先解析给定的url,然后再对其进行编码。 这避免了双重编码 import java.io.UnsupportedEncodingException; import java.net.*; public

先决条件

  • ApacheTomcat7
  • 弹簧3.2.11.释放
  • ApacheCamel 2.14.1
  • 驼峰HTTP端点(
    ..
    .com?urlparam1=value1&urlparam2=value2)作为后参数2的值

    提前谢谢

    问候,


    Max

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

    我在这里提到了如何通过驼峰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();
    
    你试过使用吗?
    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;
    }
    
    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();