【问题标题】:How to get response from DHL Api to browser如何从 DHL Api 获得对浏览器的响应
【发布时间】:2021-05-20 04:58:45
【问题描述】:

我有来自 dhl 的 api 的问题,我从 dhl 创建 GET api,当在控制台中打印时,结果会打印,但是当使用浏览器时我得到这样的响应:

com.squareup.okhttp.internal.http.RealResponseBody@68bd3d26

这是我的代码:

@RequestMapping("/getData")
public String getAcc() throws IOException
{


        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.parse("application/json");

        HttpUrl httpUrl = new HttpUrl.Builder()
                .scheme("https")
                .host("api-eu.dhl.com")
                .addPathSegment("track")
                .addPathSegment("shipments")
                .addQueryParameter("trackingNumber", "cencored")
                .addQueryParameter("service", "express")
                .build();

        Request request = new Request.Builder()
                .addHeader("content-type", "application/json")
                .addHeader("Connection", "close")
                .addHeader("DHL-API-Key", "cencored")
                .addHeader("ConsumerKey", "cencored")
                .addHeader("ConsumerSecret", "cencored")
                .removeHeader("Content-Encoding")
                .removeHeader("Content-Length")
                .url(httpUrl) // <- Finally put httpUrl in here
                .build();
      response = client.newCall(request).execute();
        System.out.println(response.body().string());
    return this.response.body().toString();
}

【问题讨论】:

    标签: spring-boot api dhl


    【解决方案1】:

    解决了... 这很奇怪,但为我工作。 所以我们不能调用“response.body().string();”两次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-12
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 2020-10-14
      • 2021-09-30
      相关资源
      最近更新 更多