POST请求发送json数据java HttpUrlConnection
我已经开发了一个Java代码,该代码使用URL和HttpUrlConnection将以下cURL转换为Java代码。卷曲是: 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"}}' 我已经编写了这段代码,但是它总是给HTTP代码400错误的请求。我找不到丢失的东西。 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 …