2013-07-23 111 views
2

我想了解如何發送https發佈請求與發佈數據使用彈簧網站或其他彈簧工具。發送https發佈請求與發佈數據使用彈簧網絡

到目前爲止,我一直在使用的HttpClient但我想轉換到春天:)

https的POST請求應該忽略自簽名證書。

請提供一個關於如何完成的例子。

謝謝

回答

3

我用Spring集成發送HTTP POST和GET http://static.springsource.org/spring-integration/reference/html/http.html 請求,工廠bean需要被配置爲允許自簽名的證書。 我使用以下接線來聲明apacheHttpsRequestFactory供http Spring Integration端點使用。

HttpClient的 Bean可以被注入到其他Spring Bean和使用發送HTTP請求:

@Autowired 
private HttpClient httpClient; 

這裏是彈簧intefration-context.xml中的片段:

<!-- HTTPS connection to trust self signed certificates --> 
<bean id="sslSocketFactory" class="org.apache.http.conn.ssl.SSLSocketFactory"> 
    <constructor-arg name="trustStrategy"> 
     <bean class="org.apache.http.conn.ssl.TrustSelfSignedStrategy" /> 
    </constructor-arg> 
    <constructor-arg name="hostnameVerifier"> 
     <bean class="org.apache.http.conn.ssl.AllowAllHostnameVerifier" /> 
    </constructor-arg> 
</bean> 
<bean id="httpsSchemaRegistry" class="org.apache.http.conn.scheme.SchemeRegistry"> 
    <property name="items"> 
     <map> 
      <entry key="https"> 
       <bean class="org.apache.http.conn.scheme.Scheme"> 

        <constructor-arg value="https" /> 
        <constructor-arg value="443" /> 
        <constructor-arg ref="sslSocketFactory" /> 
       </bean> 
      </entry> 
     </map> 
    </property> 
</bean> 
<bean id="httpClient" class="org.apache.http.impl.client.DefaultHttpClient"> 
    <constructor-arg> 
     <bean class="org.apache.http.impl.conn.PoolingClientConnectionManager"> 
      <constructor-arg ref="httpsSchemaRegistry" /> 
     </bean> 
    </constructor-arg> 
</bean> 
<bean id="apacheHttpsRequestFactory" 
    class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"> 
    <constructor-arg ref="httpClient" />