Problems sending JSON by POST when compiled

0

I have an application whose one of the modules is responsible for sending a JSON to an online API that synchronizes the data with a back-end system. When this module is executed or debugged in an IDE, it performs this task successfully, however, when executed in the application installed in Windows (.jar) it does not work and returns an API error 500, of which I have already contacted the developer of it and the same one can not make the log available to me because it is a handler. I assumed the possibility of then being permissions or some block in the firewall since in the IDE works and in the application did not, so I deactivated everything and continued with the same problem. The javax.net library is used, including in another module that is made a similar request with less data flow, it communicates with the API correctly.

Here is the snippet of the code that is being sent:

URL url = new URL(urlPost);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

    UploadRetaguardaModel upload = new UploadRetaguardaModel();
    upload.montarJsonPrincipal();
    JSONObject main = upload.mainObject;
    upload.closeConnection();

try {

    //add request header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    con.setRequestProperty("Accept-charset", "UTF-8");
    con.setRequestProperty("Content-Type", "application/json");
    con.setDoOutput(true);

    // Send post request
    OutputStream os = con.getOutputStream();
    os.write(main.toString().getBytes());
    os.close();

    int responseCode = con.getResponseCode();

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    [...]
}

Error that returns in stacktrace:

java.io.IOException: Server returned HTTP response code: 500 for URL: [...]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1926)
    at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1921)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1920)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1490)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)

Update:   This code snippet worked correctly on Linux Mint, on Windows 10 and 7 (on which it was tested) this error remains.

    
asked by anonymous 05.09.2017 / 14:30

0 answers