简体   繁体   English

在正文中使用 application/x-www-form-urlencoded 的 400 错误白色 Java Post

[英]400 error white Java Post using application/x-www-form-urlencoded in body

        BufferedReader in = null;
        String response = "";
        String urlParameters = "";
try {
        URL obj = new URL("url");
         urlParameters = "grant_type=client_credentials";
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("content-type", "application/x-www-form-urlencoded");
        con.setRequestProperty("authorization", "Basic value");

        con.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
        wr.write(urlParameters);

        // For POST only - START
        OutputStream os = con.getOutputStream();
        os.flush();
        os.close();
        // For POST only - END

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + os);
        System.out.println("Response Code================================================================================================================ : " + responseCode);

     in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;



    while ((inputLine = in.readLine()) != null) {
        response = inputLine;
    }
    }
 finally {
        if (in != null) {
            in.close();

        }
    }   }

I am using this API to get TOKEN.But getting 400 in all cases.我正在使用这个 API 来获取 TOKEN。但在所有情况下都得到 400。

As per the document API request details根据文档API 请求详细信息

Please help to solve the issue.请帮助解决问题。

I think its an issue with adding the body part ,so I appended the body part into URL,But it also failed.我认为这是添加正文部分的问题,所以我将正文部分附加到 URL 中,但它也失败了。

For this code I got error:sun.net.www.protocol.https.DelegateHttpsURLConnection对于这段代码,我得到了错误:sun.net.www.protocol.https.DelegateHttpsURLConnection

I prefer to use Apache HTTP Client library instead of the HttpURLConnection.我更喜欢使用 Apache HTTP 客户端库而不是 HttpURLConnection。 For example of the HTTP Client see https://www.baeldung.com/httpclient-4-basic-authentication例如 HTTP 客户端请参见https://www.baeldung.com/httpclient-4-basic-authentication

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FeignClient 使用 application/x-www-form-urlencoded 正文创建 POST - FeignClient create POST with application/x-www-form-urlencoded body 如何使用 webclient 发布正文 x-www-form-urlencoded? - how to post body x-www-form-urlencoded using webclient? 未找到Form&application / x-www-form-urlencoded的Body Writer - Body Writer not found for Form & application/x-www-form-urlencoded 如何使用 x-www-form-urlencoded 正文发送 post 请求 - How to send post request with x-www-form-urlencoded body 如何以 application/x-www-form-urlencoded 在 restTemplate 中发送正文 - How to send body in restTemplate as application/x-www-form-urlencoded 如何在 HTTPURLConnection 中以 application/x-www-form-urlencoded 格式向 POST 调用添加正文 - How to add a body to POST call in HTTPURLConnection in application/x-www-form-urlencoded format 找不到Java类型的消息正文编写器,而mime类型为application / x-www-form-urlencoded - A message body writer for Java type was not found mime type application/x-www-form-urlencoded JAVA RX:以相同的方法使用@PathParam和application / x-www-form-urlencoded正文 - JAVA RX : use @PathParam and application/x-www-form-urlencoded body in the same method 如何使用Android批注和RestTemplate在POST请求的正文中发送x-www-form-urlencoded - How to send x-www-form-urlencoded in a body of POST request using android annotations and resttemplate 使用 Java Unirest 发布“x-www-form-urlencoded”实体 - Post an 'x-www-form-urlencoded' entity with Java Unirest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM