java post请求json数据,POST请求发送json数据java HttpUrlConnection

I have developed a java code that convert the following cURL to java code using URL and HttpUrlConnection.

the curl is :

curl -i 'http://url.com' -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"auth": { "passwordCredentials": {"username": "adm", "password": "pwd"},"tenantName":"adm"}}'

I have written this code but it always gives HTTP code 400 bad request. I couldn't find what is missing.

String url="http://url.com";

URL object=new URL(url);

HttpURLConnection con = (HttpURLConnection) object.openConnection();

con.setDoOutput(true);

con.setDoInput(true);

con.setRequestProperty("Content-Type", "application/json");

con.setRequestProperty("Accept", "application/json");

con.setRequestMethod("POST");

JSONObject cred = new JSONObject();

JSONObject auth = new JSONObject();

JSONObject parent = new JSONObject();

cred.put("username","adm");

cred.put("password", "pwd");

auth.put("tenantName", "adm");

auth.put("passwordCredentials", cred.toString());

parent.put("auth", auth.toString());

OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());

wr.write(parent.toString());

wr.flush();

//display what returns the POST request

StringBuilder sb = new StringBuilder();

int HttpResult = con.getResponseCode();

if (HttpResult == HttpURLConnection.HTTP_OK) {

BufferedReader br = new BufferedReader(

new InputStreamReader(con.getInputStream(), "utf-8"));

String line = null;

while ((line = br.readLine()) != null) {

sb.append(line + "\n");

}

br.close();

System.out.println("" + sb.toString());

} else {

System.out.println(con.getResponseMessage());

}

解决方案

Your JSON is not correct. Instead of

JSONObject cred = new JSONObject();

JSONObject auth=new JSONObject();

JSONObject parent=new JSONObject();

cred.put("username","adm");

cred.put("password", "pwd");

auth.put("tenantName", "adm");

auth.put("passwordCredentials", cred.toString()); //

parent.put("auth", auth.toString()); //

OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream());

wr.write(parent.toString());

write

JSONObject cred = new JSONObject();

JSONObject auth=new JSONObject();

JSONObject parent=new JSONObject();

cred.put("username","adm");

cred.put("password", "pwd");

auth.put("tenantName", "adm");

auth.put("passwordCredentials", cred);

parent.put("auth", auth);

OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream());

wr.write(parent.toString());

So, the JSONObject.toString() should be called only once for the outer object.

Another thing (most probably not your problem, but I'd like to mention it):

To be sure not to run into encoding problems, you should specify the encoding, if it is not UTF-8:

con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

// ...

OutputStream os = con.getOutputStream();

os.write(parent.toString().getBytes("UTF-8"));

os.close();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值