我已经在这里尝试过关于它的每个问题,但我的代码仍然会返回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;
答案 0 :(得分:1)
我会尝试使用一个单独的工具让它工作(有很多浏览器插件可以让你创建POST请求(Postman,HttpRequester等)使用其中一个确切地知道你需要POST到什么让它工作。一旦你在其中一个工具中工作,你就可以在Java代码中做同样的事情。