POST request send json data java HttpUrlConnection
我开发了一个Java代码,它使用URL和HTTPURLCONNECT将以下卷发转换为Java代码。卷曲度为:
1 | 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错误的请求。我找不到丢失的东西。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 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 +" "); } br.close(); System.out.println("" + sb.toString()); } else { System.out.println(con.getResponseMessage()); } |
号
您的JSON不正确。而不是
1 2 3 4 5 6 7 8 9 10 11 | 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()); |
写
1 2 3 4 5 6 7 8 9 10 11 | 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()。
另一件事(很可能不是你的问题,但我想提一下):
为确保不会遇到编码问题,如果不是
1 2 3 4 5 6 7 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(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | private JSONObject uploadToServer() throws IOException, JSONException { String query ="https://example.com"; String json ="{"key":1}"; URL url = new URL(query); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestProperty("Content-Type","application/json; charset=UTF-8"); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); OutputStream os = conn.getOutputStream(); os.write(json.getBytes("UTF-8")); os.close(); // read the response InputStream in = new BufferedInputStream(conn.getInputStream()); String result = org.apache.commons.io.IOUtils.toString(in,"UTF-8"); JSONObject jsonObject = new JSONObject(result); in.close(); conn.disconnect(); return jsonObject; } |
。
您可以使用此代码通过HTTP和JSON进行连接和请求。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | try { URL url = new URL("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet" +"&key=AIzaSyAhONZJpMCBqCfQjFUj21cR2klf6JWbVSo" +"&access_token=" + access_token); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type","application/json"); String input ="{ "snippet": {"playlistId": "WL","resourceId": {"videoId": ""+videoId+"","kind": "youtube#video\ <hr> <p> the correct answer is good , but </p> [cc lang="java"]OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream()); wr.write(parent.toString()); |
。
不是为我工作,而是使用:
1 2 3 | byte[] outputBytes = rootJsonObject.getBytes("UTF-8"); OutputStream os = con.getOutputStream(); os.write(outputBytes); |
我也遇到了类似的问题,我收到了400个错误的请求,只有Put,而Post请求是完全正确的。
以下代码对该职位有效,但给出了错误的Put请求:
1 2 | conn.setRequestProperty("Content-Type","application/json"); os.writeBytes(json); |
。
在做了以下更改后,对post和put都有效
1 2 | conn.setRequestProperty("Content-Type","application/json; charset=UTF-8"); os.write(json.getBytes("UTF-8")); |