Problems sending JSON by POST when compiled

Asked

Viewed 163 times

0

I am with an application whose one of the modules is responsible for sending a JSON to an online API that synchronizes the data with a back system. When this module is run or debugged into an IDE, it performs this task successfully, but when run on the application installed on Windows (.jar) it does not work and returns a 500 API error, I have already contacted her developer and he cannot make the log available to me because it is an Handler. I considered the possibility of being either permissions or some firewall block since the IDE works and the application doesn’t, so I disabled everything and continued with the same problem. The javax.net library is used, including in another module that makes a request similar to less data flow, it communicates with the API correctly.

Follows the excerpt of the code in which the shipment is made:

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 returning 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)

Updating: This chunk of code worked correctly on Linux Mint, Windows 10 and 7 (on which they were tested) remains this error.

  • Post message with full error.

  • Try to see if the service you are consuming has crossorigin disabled.

  • right @Victorhugo, I’ll check here and I’ll give you a feedback

  • @Victorhugo, crossorigin was enabled for any request source (*) to test and the same error remained.

  • 1

    @Henrique See this: https://pt.meta.stackoverflow.com/q/5359/132

  • You can by wireshark or similar, capture the packets that travel to try to find significant differences between what the program in the JAR sends and what the program running in the IDE sends?

  • opa, truth, I hadn’t thought about it, I’m going to install here and I already give a return.

  • The wireshark helped to find some differences, however, even though leaving the identical headers remained giving error, so I switched to the apache library and was successful. Problem solved.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.