-1
我曾嘗試它的每一個問題在這裏,但我的代碼仍然返回400,爲每一個我嘗試做的請求,我沒有得到什麼,我做錯了。HttpURLConnection的POST返回400錯誤的請求
HttpURLConnection connection = null;
InputStream in = null;
try {
URL url = new URL(uri);
connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(20000);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
connection.setDoInput(true);
connection.setDoOutput(true);
String query = "";
if (Validator.isNotNull(paramsMap) && !paramsMap.isEmpty()) {
Uri.Builder builder = new Uri.Builder();
for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
builder.appendQueryParameter(entry.getKey(), entry.getValue());
}
query = builder.build().getEncodedQuery();
}
if (!query.isEmpty()) {
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
}
connection.connect();
int status = connection.getResponseCode();
if (status == 200) {
in = connection.getInputStream();
} else {
in = connection.getErrorStream();
}
} finally {
connection.disconnect();
}
return in;
你有權訪問遠程服務器上的API日誌嗎?建立連接看起來不錯,但不知道你應該怎麼來發送或您的API要求什麼,我不認爲有足夠的信息去。 –
你明白約400狀態? – Loc
@MichaelDodd它應該SE ND 3個參數,用戶名,電子郵件和grant_type,這也正是我與Uri.Builder做,我debbuged和它做對,我猜。 –