[英]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.