浏览器能正常返回,但是代码报错Server returned HTTP response code: 400 for URL
报错信息如下:
java.io.IOException: Server returned HTTP response code: 400 for URL: http://139.198.15.132:9143/tomago/v2/entities}
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at Upload.main(Upload.java:53)
代码如下:
try {
JSONObject jsonParam = new JSONObject();
jsonParam.put("callback_url","https://www.baidu.com");
JSONObject jsonMetadata = new JSONObject();
jsonMetadata.put("name","lalalaalalalalala");
jsonParam.put("metadata",jsonMetadata);
String param =jsonParam.toJSONString();
//System.out.println(param);
URL url = new URL("http://139.198.15.132:9143/tomago/v2/entities}");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
OutputStream os = connection.getOutputStream();
os.write(param.getBytes("UTF-8"));
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String lines;
StringBuffer sbf = new StringBuffer();
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sbf.append(lines);
}
String response = sbf.toString();
System.out.println(response);
connection.disconnect();
}
catch(Exception e)
{
e.printStackTrace();
}
然后要求参数是这样的:
参数:
{
"callback_url": "http://url-to-notify",
"metadata": {
"name": "xxxx",
"legal_person": "xxx",
"address": "xxx",
"telephone": "xxx",
...
}
}
花了2天时间了。。希望各位大佬帮帮我