java post json sample

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL; public class Test { public static void main(String[] args) {
try {
URL url = new URL("http://192.168.0.xx:81/OMS/Service/Mobile/MarkingService.svc/Question");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("mobiletoken", "EFF881DDCDC1FA833F3EF79EE414AC3250CA843E6B5027B516BD33F791D62E6E8DEDD421DB5EB109DCBFCB38817EEA45526D004C739DC1D75E098F65C0A8EBDFB64774E456C0F75F8A4DD6391D8AFFD497F66C2E5D508E8ECC28EE8637FC0EE69D80DF87B8A8F16474925E8217C48C6BEAEB39578C6E765259754237472CB51DBACFFB9603A8FE590642E2480531A734954424EA17B350AF8EE1B0E1523A27B97148CAD0018BC0000A175A5C3278C4D0D10F4C1106AB44B0EBF1109CDAE5057F");
conn.setRequestProperty("Content-Type","application/json; charset=UTF-8"); String Json = "{\"MarkingMode\":2,\"QuestionGroupCode\":\"61010100011701021601000301\",\"ScoreList\":[{\"MajorQuestionID\":13,\"MinorQuestionID\":0,\"Score\":2,\"StudentCode\":\"103080802011\"}]}";
byte[] writebytes = Json.getBytes();
conn.setRequestProperty("Content-Length", String.valueOf(writebytes.length));
OutputStream outwritestream = conn.getOutputStream();
outwritestream.write(Json.getBytes());
outwritestream.flush();
outwritestream.close(); if (conn.getResponseCode() == ) {
System.out.println("ok!");
} } catch (java.net.SocketTimeoutException e) {
System.out.println("Request Timeout!");
} catch (java.io.IOException e) {
System.out.println("Request Error:" + e.getMessage());
} System.out.println("End!");
} }
上一篇:Linux命令-tr


下一篇:Android 编译命令 make j8 2>&1 | tee build.log 解释