问题描述
我开发了一个java代码,使用URL和HttpUrlConnection将以下cURL转换为java代码。
curl是:
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());
}
推荐答案
您的JSON不正确。而不是
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()); // <-- toString()
parent.put("auth", auth.toString()); // <-- toString()
OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream());
wr.write(parent.toString());
写
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());
因此,对于外部对象,JSONObject.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):
要运行编码问题,应该指定编码,如果不是 UTF-8
:
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();
这篇关于POST请求发送json数据java HttpUrlConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!