0

I am trying to post json to a https secured site using apache commons httpClient library.It shows exception while posting:

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

Though I have updated security jar file by referring link SSLHandshakeException while connecting to a https site still I am getting a same exception

maven dependency

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

Code

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();
        }
    }
}
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
riya ahuja
  • 260
  • 6
  • 18
  • @Ravi I have updated the securty jars still its not working. – riya ahuja Sep 24 '17 at 06:50
  • Please go through this https://stackoverflow.com/questions/6353849/received-fatal-alert-handshake-failure-through-sslhandshakeexception – Ravi Sep 24 '17 at 07:11

0 Answers0