Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将HTTP Accept头设置为JSON将返回HTML_Java_Json_Api_Httprequest - Fatal编程技术网

Java 将HTTP Accept头设置为JSON将返回HTML

Java 将HTTP Accept头设置为JSON将返回HTML,java,json,api,httprequest,Java,Json,Api,Httprequest,我用Java编写了一个程序,从API请求JSON并将响应存储为字符串 只有当headerAccept设置为application/JSON时,我才使用return-JSON,否则返回HTML 我尝试使用setRequestProperty(“Accept”,“application/json”)设置头,但是当我调试程序时,它看起来像是实际设置了头,但出于某种原因,它仍然返回HTML 我试着和邮递员做同样的事情,结果成功了。为什么会发生这种情况 private static String

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

只有当header
Accept
设置为
application/JSON
时,我才使用return-JSON,否则返回HTML

我尝试使用
setRequestProperty(“Accept”,“application/json”)
设置头,但是当我调试程序时,它看起来像是实际设置了头,但出于某种原因,它仍然返回HTML

我试着和邮递员做同样的事情,结果成功了。为什么会发生这种情况

    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();
    }
}

我希望我已经足够具体了

可能是对象突变,我不想这样做

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的毫秒数

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

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


希望这有帮助:)

您从输入中的url得到了什么?当运行代码时,您得到了什么响应,您可以发布它吗,
301永久移动301永久移动
nginx/1.12.2
这是字符串当您运行postman时,它会重定向到安全的字符串,当您运行java时,它不会。这就是你得到html输出的原因,它说站点永久移动了,是html格式的。感谢你澄清这一点,它现在可以工作了!