天天看點

Apache Came元件http4的使用

http : For calling out to external HTTP servers using Apache HTTP Client 3.x

http4 : For calling out to external HTTP servers using Apache HTTP Client 4.x

其實是一樣的,隻是底層用的http client版本不一樣

現在以http4為例子

示例一:http調用

<route>
	<from uri="direct://start-http" />
	<!-- 指定送出方式 -->
	<setHeader headerName="CamelHttpMethod">
		<constant>POST</constant>
	</setHeader>
	<!-- 指定請求參數 -->
	<setHeader headerName="CamelHttpQuery">
		<constant>account=hongshu&client_id=111111&response_type=code</constant>
	</setHeader>
	<!-- 指定請求位址 -->
	<setHeader headerName="CamelHttpUri">
		<constant>http://www.oschina.net/action/openapi/news_list</constant>
	</setHeader>
	<!-- 指定請求的編碼方式 -->
	<setProperty propertyName="CamelCharsetName">
		<constant>UTF-8</constant>
	</setProperty>
	<!-- 執行(調用完成後,body是inputStream,轉換成string即可) -->
	<to uri="http4://http" />
	<!-- 轉換成string -->
	<convertBodyTo type="java.lang.String" charset="UTF-8" />
	<to uri="log:show?showAll=true&multiline=true" />
</route>
           

示例二:https調用

<!-- 配置ssl證書 -->
<camel:sslContextParameters id="sslContextParameters">
	<camel:trustManagers>
		<camel:keyStore resource="/opt/ssl/key/cacerts.jks" password="changeit" />
	</camel:trustManagers>
</camel:sslContextParameters>

<route>
	<from uri="direct://start-http" />
	<!-- 指定送出方式 -->
	<setHeader headerName="CamelHttpMethod">
		<constant>POST</constant>
	</setHeader>
	<!-- 指定請求參數 -->
	<setHeader headerName="CamelHttpQuery">
		<constant>account=hongshu&client_id=111111&response_type=code</constant>
	</setHeader>
	<!-- 指定請求位址 -->
	<setHeader headerName="CamelHttpUri">
		<constant>https://www.oschina.net/action/oauth2/authorize</constant>
	</setHeader>
	<!-- 指定請求的編碼方式 -->
	<setProperty propertyName="CamelCharsetName">
		<constant>UTF-8</constant>
	</setProperty>
	<!-- 執行,并且指定ssl的證書路徑 (調用完成後,body是inputStream,轉換成string即可)-->
	<to uri="http4://http?sslContextParametersRef=sslContextParameters" />
	<to uri="log:show?showAll=true&multiline=true" />
</route>