无法在Java / Netbeans中对REST API发出POST请求

时间:2015-10-01 22:08:06

标签: java rest netbeans

我需要帮助解决这个问题。
我正在向服务器发送POST请求,处理一些对象创建和编辑。 REST在Android和Android中运行良好iOS,我的桌面应用程序的代码类似 问题在于,当我尝试发送数据时,除了登录数据(也使用帖子,并且工作正常,返回令牌和所有内容)之外,我得到了这个疯狂的错误:

java.io.IOException: Server returned HTTP response code: 500 for URL: http://some.url/api/save
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1839)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)

POST方法,适用于登录:

public String POST(String targetURL, String urlParameters, int selector) {
        URL url;

        HttpURLConnection connection = null;
        try {
            url = new URL(targetURL);
            connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("POST");
            if (selector != 1) {
                connection.setRequestProperty("Authorization", "Bearer "
                        + Constantes.user.getToken());
            }
            connection.setRequestProperty("Content-Type", "plain/text");

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setReadTimeout(120000); // Send
            DataOutputStream wr = new DataOutputStream(
                    connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            // Get Response
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();
            this.setResponseCode(connection.getResponseCode());
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();
            return response.toString();

        } catch (Exception e) {
            e.printStackTrace();
            return null;

        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }

我不知道发生了什么,请帮帮我 提前谢谢!

0 个答案:

没有答案