浏览器能正常返回,但是代码报错Server returned HTTP response code: 400 for URL

RikkaTakanashi 2018-08-18 11:40:29
报错信息如下:

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天时间了。。希望各位大佬帮帮我
...全文
3170 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
verejava 2018-08-20
  • 打赏
  • 举报
回复
tianfang 2018-08-19
  • 打赏
  • 举报
回复
有更好的http post方法

https://blog.csdn.net/qq_31911463/article/details/78480991

50,504

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧