【问题标题】:Facing issue while sending a GET request with error 415发送带有错误 415 的 GET 请求时遇到问题
【发布时间】:2020-10-31 17:11:45
【问题描述】:

我正在尝试使用 HTTPUrlConnection API 连接到端点并获取 XML 格式的数据。然而,以下代码奇怪地返回 415 错误。我无法理解为什么 GET 请求会出现 415 错误。我尝试在请求中设置内容类型,但没有帮助

我也用 OkHttpClient 库尝试过同样的任务,它工作正常。我也给出了 okHttpClient 的代码:

请帮忙

    try {
         url = new URL("xxx");
     }
     catch (MalformedURLException ex) {
         ex.printStackTrace();
     }


     try {
         HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

         con.setRequestMethod("GET");

         /*con.setRequestProperty("Content-Type", "application/atom+xml;type=feed;charset=utf-8");*/


         con.connect();

         System.out.println(con.getInputStream().toString());

         /*OkHttpClient client = new OkHttpClient();

         Request request = new Request.Builder()
                 .url("xxx")
                 .method("GET", null)
                 .build();
         Response response = client.newCall(request).execute();*/

         

     } catch (IOException  ex) {

         ex.printStackTrace();
     }

【问题讨论】:

  • 接受标头怎么样?
  • 我现在感觉很蠢,是的 @zolty13 成功了。非常感谢

标签: java rest httpurlconnection


【解决方案1】:

添加 Accept 标头已解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-04
    • 2017-01-14
    • 1970-01-01
    • 2014-11-02
    • 2018-12-07
    • 1970-01-01
    • 2018-09-13
    相关资源
    最近更新 更多