有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我需要一些帮助来解决这个问题
我正在向服务器发送POST请求,处理一些对象的创建和编辑。其余的在安卓&;iOS,我的桌面应用程序也有类似的代码
问题是,当我尝试发送数据时,除了登录数据(它也使用post,并且工作正常,返回令牌和所有内容),我还遇到了一个疯狂的错误:

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) 个答案