我使用此代码段将数据发送到.xml端点(通常作为REST调用)
try {
URL url = new URL("************.xml");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
String input = "{\"mail\":\"test123@gmail.com\",\"profile_first_name\" : \"shiss\",}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes("UTF-8"));
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行此操作后给出 线程“main”中的异常java.lang.RuntimeException:失败:HTTP错误代码:302
为什么会这样