java 为 HttpURLConnection 获取 404

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25701605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:29:56  来源:igfitidea点击:

Getting 404 for HttpURLConnection

javaandroidherokuhttp-status-code-404httpurlconnection

提问by HJK

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?

我在 Heroku 中部署了一项服务,可以生成 pdf 文件输出。当我在浏览器中点击 URL 时,我可以下载 pdf 文件(我没有提示保存(根据我的要求),它会自动保存到代码中定义的路径)。因此,服务已启动并可用。但是当我使用 HttpURLConnection 访问它时,我收到了 404 错误。. 有人可以帮我解决这个问题吗?

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

以下是我正在访问的链接:http: //quiet-savannah-7144.herokuapp.com/services/time/temp

Here is the service code, deployed in Heroku server:

这是部署在 Heroku 服务器中的服务代码:

@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

注意:我的文件位于 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

我尝试使用 pdf 和 x-pdf 的内容类型,如下所示,没有任何效果

 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.

当我在我的机器的 Tomcat 服务器中本地部署服务时,一切都很好。我在过去 6 个小时里一直在努力解决这个问题,但没有任何线索。实际上我必须在android AsyncTask中获取它。如果我能够在 java 中做到这一点,那么我可以在 android 中实现相同的目标。有人可以帮我解决这个问题。

Thanks in advance

提前致谢

回答by Simone Gianni

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.

首先,如果你做一个 GET 你不应该写 conn.setDoOutput(true); 因为您没有从应用程序输出到服务器。

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.

其次,Content-Type 标头是您发送到服务器的内容的内容类型,而不是相反,因此由于您不发送任何内容而只是执行 get,因此不应设置它。

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

相反,如果您愿意,您可以设置 Accept 标头。

回答by MirMasej

Content-Typeis a server header. You should send an Acceptheader, maybe you could try something generic like Accept: *.

Content-Type是服务器标头。你应该发送一个Accept标题,也许你可以尝试一些通用的东西,比如Accept: *.