2

I have java client that hits API which i have created but when i run the class which is basically java client then it throws exception. Here is exception.

Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 405
    at TST.main(TST.java:29)

Here is the code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class TST {

    // http://localhost:8080/RESTfulExample/json/product/post
    public static void main(String[] args) {

      try {

        URL url = new URL("http://localhost:8080/JAXRS-FileUpload/rest/files/upload");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "text/plain");

        String input = "/home/hassan/Downloads/56963a249bff5_4__67_green-lantern_issue70.png";

        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        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();

     }

    }

}

I am testing it on local host i have also tried to write my IP Address in place of localhost but it does not worked it also throw the same exception.

URL url = new URL("http://192.168.1.3:8080/JAXRS-FileUpload/rest/files/upload");

Thanks in advance.

Hassan Shahbaz
  • 596
  • 1
  • 14
  • 38

1 Answers1

3

The resource you are trying to access is not there, or does not allow POST. You could try with GET to see if it is there. If not, it should return a 404.

405 return code means "Method not allowed", can be seen in some cases when the resource is simply missing. 404 means "Not Found".

Robert Bräutigam
  • 7,514
  • 1
  • 20
  • 38
  • thanks i used GET method in API now i changed it to POST but know it throws another exception – Hassan Shahbaz Feb 16 '16 at 07:21
  • Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 200 at TST.main(TST.java:29) – Hassan Shahbaz Feb 16 '16 at 07:21
  • That is actually a success code. `200` means OK. In your code you check only for "HTTP_CREATED", which is `201`. Every `2xx` code is actually a success response, so you shouldn't throw an exception for them. Some more info here: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes – Robert Bräutigam Feb 16 '16 at 07:26
  • Yes this resolve my issue i have commented the code which actually throws exception but if i want to throw exception other than 2xx then what should i do. – Hassan Shahbaz Feb 16 '16 at 07:32
  • There are no utility methods for that as far as I know, the easiest would be: `if (conn.getResponseCode()/100 > 2) { throw the exception }` – Robert Bräutigam Feb 16 '16 at 07:47