当前位置: 首页 > 面试题库 >

POST请求发送json数据java HttpUrlConnection

邢令
2023-03-14
问题内容

我已经开发了一个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 = 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不正确。代替

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()应该只为外部对象调用一次。

另一件事(可能不是你的问题,但我想提一下):

为确保不会遇到编码问题,如果不是,则应指定编码UTF-8

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

// ...

OutputStream os = con.getOutputStream();
os.write(parent.toString().getBytes("UTF-8"));
os.close();


 类似资料:
  • 我已经写了这段代码,但它总是给HTTP代码400错误的请求。我找不到丢失的东西。

  • 问题内容: 我正在尝试将一个对象作为JSON发送到Flask中的Web服务,该对象期望请求数据中包含JSON。 我已经通过发送JSON数据手动测试了该服务,并且工作正常。但是,当我尝试通过角度控制器发出http POST请求时,Web服务器向我发送一条消息,说它未接收到JSON。 当我检查Chrome中的请求标头时,似乎不是以JSON格式发送数据,而是通过内容类型将常规键/值对设置为applica

  • 我正在构建一个简单的API来测试数据库。当我使用get请求时,一切正常,但如果我更改为post,我会收到“不可处理实体”错误: 以下是FastAPI代码: 然后,我使用javascript的请求 也使用Python 我还尝试解析为 json,使用 utf-8 进行解析,并更改标头。没有什么对我有用。

  • 请求方式: "|3|2|url,content|\r" 参数: url 设置Post请求的url链接 content post请求的数据 返回值: "|3|code|data|\r" 参数: code http请求返回的成功或者错误码 成功:code = 200 获取数据失败:code = -1 http请求字段错误:code = 1 data http请求返回的数据 Arduino样例: sof

  • 问题内容: 我正在使用下面的代码发送http POST请求,该请求将对象发送到WCF服务。可以,但是如果我的WCF服务还需要其他参数怎么办?如何从Android客户端发送它们? 这是我到目前为止编写的代码: 问题答案: 使用POST发布参数: 你错过的部分在以下内容中,即如下。 其余的事情你都可以做到。

  • 我得到以下错误 响应数据为空。我做错了什么,或者我在代码中遗漏了什么?