Rohit Kaushal
Rohit Kaushal

Reputation: 51

When Posting data to URL, java.lang.RuntimeException: Failed : HTTP error code : 200.Occured..?

In the given below Class i am trying to write an JSON object into the Given HttpURL connection. But it when i try to run my class it givin me some values which i have printed but ending with an error. When i debug it in each JSON object it is getting values and at Last i have put all the JSON object into one JSON object then try to write to url but end with the following error

The main class is given below where i have written code to get my System information.here i want to get System information and then want to send that information to an url, so that it can be get from that URL. But after getting values it ends with an error.

public class NetClientPost {

    public static void main(String[] args) {


        try {

            URL url = new URL(
                    "http://projects.kpmpjdc.org/artist/artist_api/test");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            JSONObject obj = new JSONObject();




            JSONObject obj1 = new JSONObject();

            OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
            for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
                method.setAccessible(true);
                if (method.getName().startsWith("get") 
                        && Modifier.isPublic(method.getModifiers())) {
                    Object value;
                    try {
                        value = method.invoke(operatingSystemMXBean);
                    } catch (Exception e) {
                        value = e;
                    }
                    // try
                    System.out.println(method.getName() + " = " + value);
                    obj1.put(method.getName(), value);
                }}


FileSystemView filesystemBean = FileSystemView.getFileSystemView();
File[] roots = filesystemBean.getRoots();
JSONObject obj2 = new JSONObject();
obj2.put("Roots", roots[0]);

filesystemBean.getHomeDirectory();

obj2.put("HomeDirectory", filesystemBean.getHomeDirectory());
File[] f = File.listRoots();
obj.put("RD", obj2);
for (int i = 0; i<f.length; i++)
{
    JSONObject temp = new JSONObject();
    temp.put("Drive", f[i]);
    temp.put("Display name", filesystemBean.getSystemDisplayName(f[i]));
    temp.put("Is drive", filesystemBean.isDrive(f[i]));
    temp.put("Is floppy", filesystemBean.isFloppyDrive(f[i]));
    temp.put("Readable", f[i].canRead());
    temp.put("Writable", f[i].canWrite());
    temp.put("Total Space", f[i].getTotalSpace());
    temp.put("Space Use", f[i].getFreeSpace());
    temp.put("Space Free", f[i].getUsableSpace());

        obj.put("TEMP", temp);



}


            obj.put("SystemInfo1", obj1);
            OutputStreamWriter os = new OutputStreamWriter(conn.getOutputStream());
            os.write(obj.toString());
            System.out.println(obj.toString());
            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();

        }

    }

The Error comes after running it is

Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 200
    at com.wiesoftware.rest.client.NetClientPost.main(NetClientPost.java:142)

Upvotes: 2

Views: 11431

Answers (1)

Vi Matviichuk
Vi Matviichuk

Reputation: 1132

    if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

you manually throw exception here when you reply code is 200, which means absolute success. Remove that line and should work

Upvotes: 4

Related Questions