javaservlet-3.0

Java: I think I created payload for request is wrong. it is giving error Connection timed out in Java


Java: I think I created payload for request is wrong. it is giving error Connection timed out in Java

I am getting following error. java.net.ConnectException: Connection timed out. I think I am creating payload wrong. I created payload using following Curl. Please help.

curl --location --request POST 'https://Vishal.net/v1/oauth20/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_secret=d4j8Q~po9Tm3PEShtcHcaeBZW1cuYtHKcbA0FcwG' \
--data-urlencode 'client_id=9572e860-7dd7-45ff-a9c7-e9c09a915a4c' \
--data-urlencode 'scope=api://e3981169-cbc9-4e08-aa35-620714eab5bb/.default'

public String getToken() throws IOException, ServletException{

            String output = "";
            URL url = null;
            try {
                url = new URL(baseUrl+tokenSubpath);
                createLogFile(baseUrl+tokenSubpath);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                return"Error: "+ e.toString();
            }
            System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
            HttpURLConnection connection = null;
            try {
                //If we need to connect through proxy uncomment the below line
                connection = (HttpURLConnection) url.openConnection();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                return "Error: "+e.toString();
            }
            connection.setInstanceFollowRedirects(false);
            connection.setDoOutput(true); // Triggers POST.
            try {
                connection.setRequestMethod("POST");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                return "Error: "+e.toString();
            }
            String Bearer = new String(Base64.getEncoder().encode(authUserPass.getBytes()));
              //connection.setRequestProperty("Authorization", "Basic "+Bearer); //vishal bedre 8/8/2022
            connection.setRequestProperty("Authorization", "");
            
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("Content-Length", "");  //vishal bedre 8/8/2022
            connection.setRequestProperty("Host", "");  //vishal bedre 8/8/2022
            connection.setRequestProperty("Accept", "*/*");  //vishal bedre 8/8/2022
            connection.setUseCaches(false);
            
            String urlParameters="grant_type=client_credentials&client_secret=d4j8Q~po9Tm3PEShtcHcaeBZW1cuYtHKcbA0FcwG&client_id=9572e860-7dd7-45ff-a9c7-e9c09a915a4c&scope=api://e3981169-cbc9-4e08-aa35-620714eab5bb/.default";
            

            
            try(OutputStream os = connection.getOutputStream()) {
                //os.write(postData);
                OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");    
                //osw.write("grant_type=client_credentials");
                osw.write(urlParameters);
                osw.flush();
                osw.close();
                os.close();  //don't forget to close the OutputStream
                connection.connect();           
            } catch (IOException e) {
                // TODO Auto-generated catch block
                return "Error: "+e.toString();
            }
            

Solution

  • You need to look at how the data is transferred via http/https.

    1. From a URL the client first finds out the hostname and the port.
    2. It tries to resolve the hostname to IP.
    3. It creates a TCP connection to that remote ip:port
    4. if https is in use, here goes certificate checks, authentication and encryption negociation
    5. HTTP kicks in, where the client sends the request (including payload)
    6. the server sends the http response

    You are thinking about a problem in step 5 whereas your exception tells problems on step 3. So forget about the payload and check why there is a timeout. Maybe you need to open a firewall.