Search code examples
javaandroidherokuhttp-status-code-404httpurlconnection

Getting 404 for HttpURLConnection


I have a service deployed in Heroku that produces a pdf file output. When I hit the URL in the browser, I am able to download the pdf file (I am not prompting to save (as per my requirement), it auto save to defined path in the code). So service is up and available. But when I am accessing it using HttpURLConnection I am getting 404 error. . Could anyone help me out on this?

Following is the link I am accessing: http://quiet-savannah-7144.herokuapp.com/services/time/temp

Here is the service code, deployed in Heroku server:

@jawax.ws.rs.core.Context
ServletContext context;

@GET
@path("/temp")
@Produces("application/pdf")
public Response getPdf() throws IOException{
  InputStream is = context.getResourceAsStream("/static/temp.pdf");
  ResponseBuilder res = Response.ok(is);
  res.header("Content-Disposition","attachment; filename=temp.pdf");
  return res.build();
}

Note: I have my file in the location webapp/static/temp.pdf

Client code is as follows:

 try {
         URL url = new URL("http://quiet-savannah-7144.herokuapp.com/sevices/time/temp");
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
         conn.setDoOutput(true);
         conn.setRequestMethod("GET");
         conn.setRequestProperty("Content-Type", "application/json");
         int code = conn.getResponseCode();
         System.out.println("code>>"+code+"<<");
         if (conn.getResponseCode() == 200) {
            System.out.println("*************************done****************************");
            InputStream inputStream = conn.getInputStream();
            OutputStream output = new FileOutputStream("D:/copyOfTest.pdf");
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
               output.write(buffer, 0, bytesRead);
            }
            output.close();
         } else {
            Scanner scanner = new Scanner(conn.getErrorStream());
            while(scanner.hasNext())
            System.out.println(scanner.next());
            scanner.close();
         }
         conn.disconnect();
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }

I tried content type with pdf and x-pdf as shown below, nothing is working

 conn.setRequestProperty("Content-Type", "application/pdf");
 conn.setRequestProperty("Content-Type", "application/x-pdf");

When I deploy the service locally in Tomcat server in my machine, then things are absolutely fine. I am struggling from the past 6 hours to resolve this, but no clue. Actually I have to fetch it in the android AsyncTask. If I am able to do it in java, then I could achieve the same in android. Could someone help me out on this.

Thanks in advance


Solution

  • I see a few problems.

    First, if you do a GET you should not write conn.setDoOutput(true); cause you're not outputting from your application to the server.

    Second, the Content-Type header is the content-type of what YOU send to the server, not the opposite, so since you're not sending anything but just doing a get, you should not set it.

    Instead, maybe, if you want, you can set the Accept header.