Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.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 使用httpurlconnection调用第三方api_Java_Spring_Rest_Httpurlconnection - Fatal编程技术网

Java 使用httpurlconnection调用第三方api

Java 使用httpurlconnection调用第三方api,java,spring,rest,httpurlconnection,Java,Spring,Rest,Httpurlconnection,我试图从我的控制器调用第三方api以获取令牌(POST请求),但得到400,我们使用的是java.net.HttpURLConnection 我尝试添加不同类型的头使用,但没有运气,我也尝试与RESTTemplate,但相同的问题。第三方提供的招摇过市的用户界面也是一样,我在互联网上搜索并尝试了不同的解决方案,但运气不好 public String getToken(String requestJsonString) { String responseJSONString = "

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

我尝试添加不同类型的头使用,但没有运气,我也尝试与RESTTemplate,但相同的问题。第三方提供的招摇过市的用户界面也是一样,我在互联网上搜索并尝试了不同的解决方案,但运气不好

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是正确的,正如我在swagger中尝试的那样,可能是缺少请求头,请提供专业帮助。我希望返回200个。

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

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

现在它工作了

您是否尝试将字节写入“UTF-8”中的
OutputStream
os.write(requestJsonString.toString().getBytes(“UTF-8”))
我试过了,同样的问题,感谢您对HTTP 400代码bean“错误请求”的评论。它可以是任何东西,这取决于在服务器端执行的验证检查。尝试使用
curl
或Postman实现有效请求,然后检查使用Java程序时的区别。除了错误代码之外,您是否还收到错误消息?我尝试过Postman获取400个无效请求,现在我将从这里进行调试,但是否知道缺少什么,包括内容类型