【问题标题】:sending https post request with post data using spring web使用spring web发送带有post数据的https post请求
【发布时间】:2013-07-23 09:14:51
【问题描述】:

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

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

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

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

谢谢

【问题讨论】:

    标签: java spring


    【解决方案1】:

    我使用 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" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-06
      • 2011-10-18
      • 2021-06-03
      • 2015-07-22
      • 2019-12-04
      • 1970-01-01
      相关资源
      最近更新 更多