Can';t在Java/Netbeans中对RESTAPI发出POST请求
我需要一些帮助来解决这个问题Can';t在Java/Netbeans中对RESTAPI发出POST请求,java,rest,netbeans,Java,Rest,Netbeans,我需要一些帮助来解决这个问题 我正在向服务器发送POST请求,处理一些对象的创建和编辑。其余的在Android和iOS中运行良好,我的桌面应用程序也有类似的代码。 问题是,当我尝试发送数据时,除了登录数据(它也使用post,并且工作正常,返回令牌和所有内容),我还遇到了一个疯狂的错误: java.io.IOException: Server returned HTTP response code: 500 for URL: http://some.url/api/save at sun.
我正在向服务器发送POST请求,处理一些对象的创建和编辑。其余的在Android和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();
}
}
}
我不知道发生了什么事,请帮帮我。提前谢谢 那台RESTAPI服务器是你的吗?最好先检查一下,为什么服务器不喜欢您的请求(即抛出500)。顺便问一下,响应是否包含状态代码500以外的任何内容?它通常在响应正文中包含一些错误消息。是的,服务器也是我的,并且在android和ios请求中都工作得很好。。这个错误,这是我能看到的唯一一个好的,这是你的。而不仅仅是当你从桌面应用程序发送请求时,检查那里发生了什么错误,这会导致500个错误。当出现任何错误时,Web服务器通常会响应500,所以只需检查日志即可。没有别的办法,那只是猜测而已。你需要知道,怎么了。好吧,让我检查一下日志,我会回来的:你再也没有回来过。