简体   繁体   中英

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

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

由于服务器或服务器应用程序在处理您的请求时发生内部错误。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM