版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Andrew_Yuan/article/details/84617542
第一种: public class HttpClientUtil { public static String sendAndRecv(String uri,String charset,String json){ HttpClientParams clientParams = new HttpClientParams(); clientParams.setSoTimeout(45000); clientParams.setContentCharset("utf-8"); HttpClient client = new HttpClient(clientParams); PostMethod method = new PostMethod(uri); try{ method.setRequestEntity(new StringRequestEntity(json,"text/json",charset)); int result = client.executeMethod(method); if(result == 200){ InputStream is = method.getResponseBodyAsStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i=-1; while((i=is.read())!=-1){ baos.write(i); //baos.flush(); } String response =baos.toString(charset); return response; }else{ System.out.println(result); } }catch (Exception e) { e.printStackTrace(); }finally{ method.releaseConnection(); } return "-1"; } public static void main(String[] args){ JSONObject json = new JSONObject(); JSONObject reqstr = new JSONObject(); // reqstr.put("unitid", "1019"); // reqstr.put("unitname", "钉钉部门新增同步实测"); // reqstr.put("parentunitid", "0001"); // reqstr.put("unitlevel", "2"); reqstr.put("deptId", "1080"); reqstr.put("deptName", "新增部门联通测试"); reqstr.put("deptlevel", "2"); reqstr.put("supDeptId", "1"); reqstr.put("picName", "zhy"); reqstr.put("contact","zhy"); reqstr.put("deptDiscription","zaq"); json.put("reqstr", reqstr); json.put("appid", "100000000001"); json.put("appseq", "4"); json.put("txntype","2001"); BangsunHttpClientUtil.sendAndRecv("http://ip地址加端口/dept/deptadd","UTF-8",json.toString()); } }
第二种:
public class BangsunUtiltest { public static String sendAndRecv(String uri,String charset,String json){ CloseableHttpClient client = HttpClientBuilder.create().build(); HttpPost method = new HttpPost(uri); StringEntity entity = new StringEntity(json,"UTF-8"); method.setEntity(entity); method.setHeader("Content-Type","application/json;charset=utf8"); CloseableHttpResponse response = null; try{ response = client.execute(method); int result = response.getStatusLine().getStatusCode(); HttpEntity httpEntity = response.getEntity(); if(result == 200){ System.out.println(EntityUtils.toString(httpEntity)); // if(httpEntity != null){ // System.out.println("result: " + EntityUtils.toString(httpEntity, "UTF-8")); // } return null; }else if(result == 302){ System.out.println(response.getFirstHeader("location")); // if(httpEntity != null){ // System.out.println("result: " + EntityUtils.toString(httpEntity, "UTF-8")); // }else{ // System.out.println("无返回内容!"); // } }else{ System.out.println(result); } }catch (Exception e) { e.printStackTrace(); }finally{ method.releaseConnection(); // if(client != null){ // try { // client.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // // if(response != null){ // try { // response.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } } return "-1"; } public static void main(String[] args){ JSONObject json = new JSONObject(); JSONObject reqstr = new JSONObject(); // reqstr.put("deptId", "1090"); reqstr.put("deptName", "新增部门联通测试1"); reqstr.put("deptlevel", "2"); reqstr.put("superDeptId", "0"); reqstr.put("picName", "zhy"); reqstr.put("contact","zhy"); reqstr.put("deptDiscription","zaqy"); //json.put("reqstr", reqstr); // // // JSONObject json = new JSONObject(); // JSONObject reqstr = new JSONObject(); // reqstr.put("userId", "20226"); // reqstr.put("realName", "江小鱼1"); // reqstr.put("sex", "1"); // reqstr.put("mobile", "201236541"); // reqstr.put("usergrade","2"); // reqstr.put("deptId","1"); // reqstr.put("workstation","合肥"); // reqstr.put("rntryDate","2018-11-15T01:27:23.621Z"); // reqstr.put("userName","花撒1"); // reqstr.put("title","sss"); // reqstr.put("parauserid","10004"); // reqstr.put("email","[email protected]"); // reqstr.put("socAge","25"); // reqstr.put("comAge","2"); // reqstr.put("openid","25567"); // reqstr.put("dtUserId","1"); // reqstr.put("workStation","合肥"); String jsonString = JSON.toJSONString(reqstr); System.out.println(jsonString); BangsunUtiltest.sendAndRecv("http://ip/dept/deptadd","UTF-8",jsonString); } }