java - 使用 spring web 发送带有 post 数据的 https post 请求

标签 java spring

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

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

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

请提供一个示例来说明如何完成此操作。

谢谢

最佳答案

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

httpClient bean 可以注入(inject)其他 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" />

关于java - 使用 spring web 发送带有 post 数据的 https post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17806054/

相关文章:

java - JDBC 连接已连接但没有查询结果到结果集

java - Ant 编译但不运行或创建 jar

java - StackOverflowError 尝试在 Spring WebSecurityConfigurerAdapter 中公开 AuthenticationManager

java - 在 servlet-context 中的 dev 和 prod 值之间切换的标志

Java 并发数 : is final field (initialized in constructor) thread-safe?

java - 打印到终端而不滚动

java - 找不到提供程序 org.glassfish.json.JsonProviderImpl

java - 如何解决hibernate建表时提示创建表不成功的问题

json - 使用 Spring/JPA 写入 Postgres 数据库的 JSON 列

java - Micronaut - Springframework @Bean 等效项是什么?