[英]FeignClient create POST with application/x-www-form-urlencoded body
[英]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();
}
} }
我正在使用這個 API 來獲取 TOKEN。但在所有情況下都得到 400。
根據文檔API 請求詳細信息
請幫助解決問題。
我認為這是添加正文部分的問題,所以我將正文部分附加到 URL 中,但它也失敗了。
對於這段代碼,我得到了錯誤:sun.net.www.protocol.https.DelegateHttpsURLConnection
我更喜歡使用 Apache HTTP 客戶端庫而不是 HttpURLConnection。 例如 HTTP 客戶端請參見https://www.baeldung.com/httpclient-4-basic-authentication
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.