使用spring web发送带有帖子数据的https发布请求

时间:2013-07-23 09:14:52

标签: java spring

我正在尝试了解如何使用弹簧网或其他弹簧工具发送带有帖子数据的https发布请求。

到目前为止,我一直在使用httpclient,但我正在尝试转换为spring:)

https发布请求应忽略自签名证书。

请提供一个如何完成的示例。

谢谢

1 个答案:

答案 0 :(得分:3)

我使用Spring Integration发送http POST和GET http://static.springsource.org/spring-integration/reference/html/http.html 需要将request-factory bean配置为允许自签名证书。 我使用以下连接声明 apacheHttpsRequestFactory 以供http Spring Integration端点使用。

httpClient bean可以注入其他Spring Bean并用于发送http请求:

@Autowired
private HttpClient httpClient;

这是spring-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" />