HTTP错误代码:302发送数据时

时间:2012-08-16 05:42:25

标签: java json http rest http-post

我使用此代码段将数据发送到.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

为什么会这样

0 个答案:

没有答案