2

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

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

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

请举例说明如何做到这一点。

谢谢你

4

1 回答 1

3

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

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" />
于 2014-03-01T15:15:49.150 回答