【问题标题】:calling third party api using httpurlconnection使用 httpurlconnection 调用第三方 api
【发布时间】:2020-01-03 15:22:22
【问题描述】:

我正在尝试从我的控制器调用第三方 api 以获取令牌(POST 请求)但得到 400,我们正在使用 java.net.HttpURLConnection

我尝试添加使用的不同类型的标头但没有运气,我也尝试使用 RESTTemplate 但同样的问题。同样在第三方提供的招摇 UI 中工作,我在互联网上搜索并尝试了不同的解决方案,但没有运气。

public String getToken(String requestJsonString) {
        String responseJSONString = "";
        URL constructedURL = null;
        HttpURLConnection httpConnection = null;
        String url = null;

        url = "any url"; //dummy
        try {
            constructedURL = new URL(url);
            httpConnection = (HttpURLConnection) constructedURL.openConnection();
            httpConnection.setDoOutput(Boolean.TRUE);
            httpConnection.setRequestMethod(RaterConstants.POST);
            httpConnection.setRequestProperty("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
            httpConnection.setRequestProperty("accept", RaterConstants.APPLICATION_JSON);

            OutputStream os = httpConnection.getOutputStream();
            os.write(requestJsonString.getBytes());
            os.flush();

            InputStream inputStream;
            if (httpConnection.getResponseCode() >= 400) {
                inputStream = httpConnection.getErrorStream();
            } else {
                inputStream = httpConnection.getInputStream();
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((inputStream)));
            String output;
            while ((output = br.readLine()) != null) {
                responseJSONString = output;
            }
            httpConnection.disconnect();
        }catch (IOException e) {
            logger.error("Error occurred in AccessTokenData : getToken : " + e.getCause());
        }
        return responseJSONString;
    }

来自 swagger UI 的请求标头 - 从开发工具中获取

POST /Test HTTP/1.1
Host: host
Connection: keep-alive
Content-Length: 87
accept: application/json
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
Sec-Fetch-Mode: cors
Content-Type: application/x-www-form-urlencoded
Origin: url
Sec-Fetch-Site: same-origin
Referer: url
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: PF=Ycbz2Gt6Bwyeguyegyue

requestJsonString 是正确的,因为我尝试过招摇,可能是缺少请求标头,请帮助您的专业知识。我希望返回 200。

【问题讨论】:

  • 您是否尝试将字节写入“UTF-8”中的OutputStreamos.write(requestJsonString.toString().getBytes("UTF-8"))
  • 我试过了,同样的问题,谢谢你的评论
  • HTTP 400 代码 bean “错误请求”。它可以是任何东西,具体取决于在服务器端完成的验证检查。尝试使用curl 或 Postman 实现有效请求,然后检查您使用 Java 程序时的不同之处。
  • 你除了错误码还有错误信息吗?
  • 我试过邮递员收到 400 个无效请求,现在我将从这里调试,但不知道会丢失什么,包括 content-Type

标签: java spring rest httpurlconnection


【解决方案1】:

谢谢大家的回复。 根本原因:Content-Type: application/x-www-form-urlencoded 不适用于 JSON。 我将 JSON 更改为普通字符串,使用“&”作为分隔符,如下所示:

requestJsonString = "grant_type=abc&client_id=id_123&secret=demoSecret"

现在可以了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-02
    • 2020-11-08
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 2022-11-14
    • 2020-06-28
    相关资源
    最近更新 更多