发布到https网站

时间:2017-09-24 06:42:57

标签: java httpclient apache-httpclient-4.x apache-commons-httpclient

我正在尝试使用apache commons httpClient库将json发布到https安全站点。它在发布时显示异常:

java.lang.IllegalStateException: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

虽然我通过引用链接SSLHandshakeException while connecting to a https site更新了安全jar文件,但我仍然得到了同样的异常

maven依赖

<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
</dependency>

代码

package com.sterling.smartdata.webapi.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultHttpSender implements HttpSender {
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultHttpSender.class);
    private HttpClient httpClient;

    public WebResponse postHttpJson(String url, String path, Map<String, String> headers, String msg) {
        PostMethod post = new PostMethod(url);

        try {

            StringRequestEntity entity = new StringRequestEntity(msg);
            post.setRequestEntity(entity);
            post.setRequestHeader("accept", "application/json");
            post.setRequestHeader("content-type", "application/json");
            if (path != null) {
                post.setPath(path);
            }

            if (headers != null && !headers.isEmpty()) {
                for (Entry<String, String> entry : headers.entrySet())
                    post.setRequestHeader(entry.getKey(), entry.getValue());
            }
            int statusCode = httpClient.executeMethod(post);

            String response = post.getResponseBodyAsString();
            LOGGER.debug("Web Call to url " + url + " returned code " + statusCode + " respnose " + response);
            WebResponse webResponse = new WebResponse();
            webResponse.setResponse(response);
            webResponse.setCode(statusCode);
            return webResponse;
        } catch (HttpException httpex) {
            throw new IllegalStateException(httpex);
        } catch (IOException ioex) {
            throw new IllegalStateException(ioex);
        } finally {
            post.releaseConnection();
        }
    }
}

0 个答案:

没有答案