Error: javax.net.ssl.SSLHandshakeException when accesing to HTTPS site from Lambda

I'm developing a custom skill to Alexa (Amazon), and I coded the skill in lambda AWS service in JAVA 8. I'm trying to get a simple response from my ask2decide API and it's not working.

Basically I'm executing the following code:

String questiont = "casco"
String queryString = 
        String.format("https://www.ask2decide.com/api/prod/search.php?s=%s", questiont);

    InputStreamReader inputStream = null;
    BufferedReader bufferedReader = null;
    StringBuilder builder = new StringBuilder();
    try {
        String line;
        URL url = new URL(queryString);
        inputStream = new InputStreamReader(url.openStream(), Charset.forName("US-ASCII"));
        bufferedReader = new BufferedReader(inputStream);
        while ((line = bufferedReader.readLine()) != null) {
            builder.append(line);
            System.out.println(line);
        }
    } catch (IOException e) {
        // reset builder to a blank string
        System.out.print("ERROR:");
        System.out.println(e);

    } finally {
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(bufferedReader);
    }

The result of executing it is the following

error:"javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake"

Note: I get a JSON standard array if I execute the URL in Chrome without any error.

Note2: The imports:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.*;
import javax.net.ssl.*;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.amazon.speech.slu.Intent;
import com.amazon.speech.slu.Slot;
import com.amazon.speech.speechlet.IntentRequest;
import com.amazon.speech.speechlet.LaunchRequest;
import com.amazon.speech.speechlet.Session;
import com.amazon.speech.speechlet.SessionEndedRequest;
import com.amazon.speech.speechlet.SessionStartedRequest;
import com.amazon.speech.speechlet.SpeechletV2;
import com.amazon.speech.speechlet.SpeechletResponse;
import com.amazon.speech.json.SpeechletRequestEnvelope;
import com.amazon.speech.ui.OutputSpeech;
import com.amazon.speech.ui.PlainTextOutputSpeech;
import com.amazon.speech.ui.SsmlOutputSpeech;
import com.amazon.speech.ui.Reprompt;
import com.amazon.speech.ui.SimpleCard;
import com.amazonaws.util.json.JSONArray;
import com.amazonaws.util.json.JSONException;
import com.amazonaws.util.json.JSONObject;
import com.amazonaws.util.json.JSONTokener;
  • Java 8: Filter an Array(NxM) to create a Map<String, HashSet<String>>

    I would like to map an NxN array into a Map in Java 8.

    The idea is that every [i][0] element is a key and every [i][j] with j>0 is a list of values for every key in the map.

    Thanks for any help. :)

    This is my class:

    public class GroupingDishes {
    
        public static void main(String[] args) {
    
            String[][] dishes = {
                    {"Salad", "Tomato", "Cucumber", "Salad", "Sauce"},
                    {"Pizza", "Tomato", "Sausage", "Sauce", "Dough"},
                    {"Quesadilla", "Chicken", "Cheese", "Sauce"},
                    {"Sandwich", "Salad", "Bread", "Tomato", "Cheese"}
            };
    
            Map<String, HashSet<String>> groupDishes = groupingDishes(dishes);
        }
    
        public static Map<String, HashSet<String>> groupingDishes(String[][] dishes) {
    
            Map<String, HashSet<String>> mapFood = new HashMap<>();
    
            for (int i = 0; i < dishes.length; i++) {
    
                String food = dishes[i][0];
    
                for (int j = 0; j < dishes[i].length; j++) {
    
                    if (mapFood.containsKey(food)) {
    
                        HashSet<String> existingIngredients = mapFood.get(dishes[i][0]);
                        existingIngredients.add(dishes[i][j]);
                        mapFood.put(food, existingIngredients);
    
                    } else {
    
                        HashSet<String> newIngredient = new HashSet<>();
                        mapFood.put(food, newIngredient);
    
                    }
                }
            }
            return mapFood;
        }
    }
    
  • Getting unhandled IOException on globally defined variable - JAVA

    How can one stop the IOException question being raised on something as such:

    public class ExampleClass {
    
       String path = "path to directory";
       Path filePath = Paths.get(path);
       BasicFileAttributes Attr = Files.readAttributes(filePath, BasicFileAttributes.class);
    
       ...
    
    }
    

    I receive a warning from IntelliJ stating Unhandled exception: java.io.IOException on the last line BasicFileAttributes inAttr = Files.readAttributes(filePathIn, BasicFileAttributes.class);

    If I put these declarations within a method that throws an IOException, the warning does go away... But I am wanting this declared as a global variable... Am I missing something?

  • How to handle an IOException thrown by an HttpClient?

    sometimes when trying to call an api from background service of my app, connection is not established and I can see IOException messages(Network error) from my logs.

    AndroidHttpClient client = null;
            try {
                // Create the end-point client
                client = AndroidHttpClient.newInstance("TCR Software Android Application");
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 30000); // 10000
                HttpConnectionParams.setSoTimeout(client.getParams(), 30000); // 10000
    
                // Execute the request
                Log.v("RESTClient", "executing " + request.getMethod() + " " + request.getURI());
                HttpResponse response = client.execute(request);
    
                // Get response wrapper
                responseBody = EntityUtils.toString(response.getEntity());
                JSONObject responseWrapper = new JSONObject(responseBody);
                //JSONObject responseWrapper = new JSONObject(EntityUtils.toString(response.getEntity()));
    
                // Check result type
                if (!responseWrapper.has("ResultType")) throw new RESTClientException(responseWrapper.getString("Message"), Reason.SERVICE_ERROR);
    
                // Get the data object
                Object responseData = responseWrapper.isNull("Data") ? null : responseWrapper.get("Data");
    
                // Check for su
                if (responseWrapper.getInt("ResultType") != 1) throw new RESTClientException(responseData == null ? responseWrapper.getString("Message") : responseData.toString(), Reason.APPLICATION_ERROR);
    
                // Always return JSONArray
                if (responseData == null)
                    return new JSONArray();
                if (responseData instanceof JSONObject)
                    return new JSONArray().put(responseData);
                else if (responseData instanceof JSONArray)
                    return (JSONArray)responseData;
                else if (responseData instanceof Boolean)
                    return new JSONArray().put(responseData);
                else if (responseData instanceof Date)
                    return new JSONArray().put(responseData);
                else if (responseData instanceof String)
                    return new JSONArray().put(responseData);
                else if (responseData instanceof Integer)
                    return new JSONArray().put(responseData);
                else
                {
                    Log.e("RESTClient", "Unexpected data format returned by server\n" + responseBody);
                    throw new RESTClientException("Unexpected data format returned by server. ", Reason.PROTOCOL_ERROR);
                }
            } finally {
                if (client != null) client.close();
            }
        } catch (JSONException e) {
            Log.e("RESTClient", "Unintelligible response from server\n" + responseBody, e);
            throw new RESTClientException("Unintelligible response from server", Reason.PROTOCOL_ERROR, e);
        } catch (ClientProtocolException e) {
            throw new RESTClientException("Incompatible server protocol", Reason.SERVICE_ERROR, e);
        } catch (UnsupportedEncodingException e) {
            throw new RESTClientException("Failure encoding request data", Reason.INTERNAL_ERROR, e);
        } catch (IOException e) {
            throw new RESTClientException("Network error", Reason.NO_CONNECTION);
        }
    

    I am displaying the Network error message if IOException occurs. But how to fix this issue to not happen. Does increasing setConnectionTimeout, setSoTimeout values to 60 seconds helps to solve this?