我必须传递很长的OpenStreetMapsoverpass api url请求。由于 GET 请求太长,我决定使用 POST 请求。不幸的是,更改 RequestMethod 会解析 400 个错误代码(使用 GET 方法相同的查询会产生 200 个代码(。
这是我的HttpURLConnection代码:
public String downloadUrl(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try{
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
Log.wtf("JSON","connection started...");
// Connecting to url
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.connect();
Log.wtf("KOD",Integer.toString(urlConnection.getResponseCode()));
// Reading data from url
iStream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuilder sb = new StringBuilder();
String line = "";
while( ( line = br.readLine()) != null){
sb.append(line);
}
data = sb.toString();
br.close();
}catch(Exception e){
Log.wtf("ExceptionWhileUrlDownload", e.toString());
}finally{
iStream.close();
urlConnection.disconnect();
}
return data;
}
删除应用程序/json
从 URL 中删除查询。然后将该查询字符串写入输出流。之后,您可以从输入流中读取
>HTTP 错误 400 是错误的请求错误。这意味着您发送了错误的请求。当您使用 GET 获得 HTTP 200 和带有 POST 的 HTTP 400 时,您调用的 HTTP 方法是 GET 而不是 POST。因此,您不能向GET方法发送POST请求。