【问题标题】:Setting HTTP Accept header to JSON returns HTML将 HTTP Accept 标头设置为 JSON 会返回 HTML
【发布时间】:2018-04-14 14:57:03
【问题描述】:

我用 Java 编写了一个从 API 请求 JSON 并将响应存储为字符串的程序。

我使用的 API 仅在标头 Accept 设置为 application/json 时返回 JSON,否则返回 HTML。

我尝试使用setRequestProperty("Accept", "application/json") 设置标题,但是当我调试程序时,看起来标题实际上已设置,但由于某种原因它仍然返回 HTML。

我尝试用 Postman 做同样的事情,它工作正常。为什么会这样?

    private static String readUrl(String urlString) throws Exception {
    BufferedReader reader = null;
    try {
        URL url = new URL(urlString);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestProperty("Accept", "application/json");
        reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        StringBuffer buffer = new StringBuffer();
        int read;
        char[] chars = new char[1024];
        while ((read = reader.read(chars)) != -1)
            buffer.append(chars, 0, read);

        return buffer.toString();
    } finally {
        if (reader != null)
            reader.close();
    }
}

我希望我已经足够具体了

【问题讨论】:

  • 你从输入中的 url 得到什么?
  • 你在运行代码的时候得到了什么响应,你能发一下吗,
  • <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.12.2</center> </body> </html> 这是字符串
  • 当你通过邮递员运行时,它会重定向到安全的trola.si/bavarski,而当你通过java运行时它不会。这就是您在 html 中获得输出的原因,该输出表明站点已永久移动,采用 html 格式
  • 感谢您的澄清,现在可以使用了!

标签: java json api httprequest


【解决方案1】:

邮递员 :邮递员在内部使用重定向,打开 拦截器并在设置中的常规选项卡下关闭重定向

YourCode:将 url 更改为安全的,使用协议 https

希望这会有所帮助:)

【讨论】:

    【解决方案2】:

    可能是对象突变,我通过这种方式,

    public String getJSON(String url, int timeout) {
    HttpURLConnection c = null;
    try {
        URL u = new URL(url);
        c = (HttpURLConnection) u.openConnection();
        c.setRequestMethod("GET");
        c.setRequestProperty("Content-length", "0");
        c.setUseCaches(false);
        c.setAllowUserInteraction(false);
        c.setConnectTimeout(timeout);
        c.setReadTimeout(timeout);
        c.connect();
        int status = c.getResponseCode();
    
        switch (status) {
            case 200:
            case 201:
                BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line+"\n");
                }
                br.close();
                return sb.toString();
        }
    
    } catch (MalformedURLException ex) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
    } finally {
       if (c != null) {
          try {
              c.disconnect();
          } catch (Exception ex) {
             Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
          }
       }
    }
    return null;
    }
    

    这是一个函数,更笼统地说,在函数的第一个参数中传递你的 url,第二个,传递你想要访问该 url 的毫秒数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 2010-11-11
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      相关资源
      最近更新 更多