2017-02-14 2 views
3

Я бегу ниже код на Java 8,Полученное фатальное предупреждение: handshake_failure: javax.net.ssl.SSLHandshakeException

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.nio.charset.Charset; 

import org.apache.commons.codec.binary.Base64; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.HttpClientBuilder; 
import org.json.JSONObject; 

public class HttpPostReq { 
public static void main(String args[]) { 
    String restUrl = "https://connect-testing.urbanairship.com/api/events/"; 
    String username = "sample_connection"; 
    String password = "sample_connection"; 
    JSONObject jsonToSend = new JSONObject(); 
    String jsonData = jsonToSend.toString(); 
    HttpPostReq httpPostReq = new HttpPostReq(); 
    System.out.println(jsonData); 
    HttpPost httpPost = httpPostReq.createConnectivity(restUrl, username, password); 
    System.out.println(httpPost); 
    httpPostReq.executeReq(jsonData, httpPost); 
} 

HttpPost createConnectivity(String restUrl, String username, String password) { 
    HttpPost post = new HttpPost(restUrl); 
    String auth = new StringBuffer(username).append(":").append(password).toString(); 
    byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII"))); 
    String authHeader = "Basic " + new String(encodedAuth); 
    System.out.println(authHeader); 
    post.setHeader("Authorization", authHeader); 
    post.setHeader("Content-Type", "application/x-www-form-urlencoded"); 
    post.setHeader("Accept", "application/vnd.urbanairship+x-ndjson; version=3;"); 
    post.setHeader("X-Stream", "true"); 
    return post; 
} 

void executeReq(String jsonData, HttpPost httpPost) { 
    try { 
     executeHttpRequest(jsonData, httpPost); 
    } catch (UnsupportedEncodingException e) { 
     System.out.println("error while encoding api url : " + e); 
    } catch (IOException e) { 
     System.out.println("ioException occured while sending http request : " + e); 
    } catch (Exception e) { 
     System.out.println("exception occured while sending http request : " + e); 
    } finally { 

    } 
} 

void executeHttpRequest(String jsonData, HttpPost httpPost) throws UnsupportedEncodingException, IOException { 
    HttpResponse response = null; 
    String line = ""; 
    StringBuffer result = new StringBuffer(); 
    httpPost.setEntity(new StringEntity(jsonData)); 
    HttpClient client = HttpClientBuilder.create().build(); 
    response = client.execute(httpPost); 
    System.out.println("Post parameters : " + jsonData); 
    System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
    while ((line = reader.readLine()) != null) { 
     result.append(line); 
    } 
    System.out.println(result.toString()); 
} 
} 

Getting исключение как,

IOException произошло во время отправки запроса HTTP: javax.net.ssl.SSLHandshakeException: Получено фатальное предупреждение: handshake_failure.

Я могу подключиться к через завиток,

curl -vv https://connect-testing.urbanairship.com/api/events/ \ 
--compressed \ 
-u "sample_connection:sample_connection" \ 
-H "Accept: application/vnd.urbanairship+x-ndjson; version=3;" \ 
-d "{}" 

Может кто-нибудь сказать мне, что случилось с моим кодом Java?

Зависимости, как показано ниже,

<dependencies> 
    <dependency> 
     <groupId>org.json</groupId> 
     <artifactId>json</artifactId> 
     <version>20090211</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.httpcomponents</groupId> 
     <artifactId>httpclient</artifactId> 
     <version>4.5.2</version> 
    </dependency> 

ответ

3

Может быть проблема совместимости между версией протокола TLS или шифров, используемых клиентом и сервером.

+0

Любое решение для этого? – shiv

+0

Вы можете попробовать использовать TLSv1 или TLSv1.1 через TLSv1.2. Найдено [эта страница] (http://www.oracle.com/technetwork/java/javase/documentation/cve-2014-3566-2342133.html), которая рекомендует использовать 'java.lang.System.setProperty (« https.protocols » "," TLSv1, TLSv1.1, TLSv1.2 "); – mwstc013

Смежные вопросы