为什么我收到500错误以响应我的JSON请求?

时间:2013-12-02 12:11:17

标签: java json post urlconnection

我正在尝试将JSON发送到服务器,但它不断返回500错误。

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

错误

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

2 个答案:

答案 0 :(得分:0)

因为服务器或服务器应用程序在处理您的请求时遇到内部错误。

答案 1 :(得分:0)

此错误只能通过修复Web服务器软件来解决。这不是客户端问题。由Web服务器站点的操作员来定位和分析日志,这些日志应提供有关错误的更多信息。