2013-07-23 79 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" />