Why am I receiving 500 error in response to my JSON request?

748 Views Asked by At

I am trying to send the a JSON to server but it constantly returns 500 error.

java.lang.String contentToPost = jsarray.toJSONString();
        java.net.URLConnection connection = new java.net.URL("http://example.com/service?apiKey=12345").openConnection();
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Length", "" + contentToPost.length());
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Cache-Control", "no-cache");

        java.io.OutputStream stream = connection.getOutputStream();
        stream.write(contentToPost.getBytes());
        stream.close();
        System.err.println("request is sent");
        // Read the response
        java.io.BufferedReader br = new java.io.BufferedReader(new  java.io.InputStreamReader(connection.getInputStream()));
        java.lang.StringBuffer sb = new java.lang.StringBuffer();
        java.lang.String str = br.readLine();
        while (str != null) {
            sb.append(str);
            str = br.readLine();
        }
        br.close();
        java.lang.String responseString = sb.toString();
        System.err.println(responseString);

Error

java.io.IOException: Server returned HTTP response code: 500 for URL: http://example.com/service?apiKey=12345
2

There are 2 best solutions below

0
On

Because the server or server application had an internal error handling your request.

0
On

This error can only be resolved by fixes to the Web server software. It is not a client-side problem. It is up to the operators of the Web server site to locate and analyse the logs which should give further information about the error.