HttpClient访问restful

JSONObject object = new JSONObject();object.put("nickName", "你好");HttpClient httpClient = new DefaultHttpClient();HttpPost httpPost = new HttpPost("https://rest.kitsmart.cn/api/account/account_user/2cc08085549e494a0154a2be69070036?_method=PATCH&access_token=f0960af0-b24e-4153-abf1-c33a97ac3fb2");//HttpPost httpPost = new HttpPost("http://localhost:3333/account_user/2cc08085549e494a0154a2be69070036?_method=PATCH&access_token=f0960af0-b24e-4153-abf1-c33a97ac3fb2");StringEntity s = new StringEntity(object.toString(), Charset.forName("utf-8"));s.setContentEncoding("UTF-8");s.setContentType("application/json;charset=utf-8");//发送json数据需要设置contentTypehttpPost.addHeader(new BasicHeader("Accept","application/json;charset=utf-8"));httpPost.setEntity(s);String result = null;HttpResponse response = httpClient.execute(httpPost);if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, "UTF-8"); }}System.out.println("++ ===> " + result);


注意--

几个header的写法
还有utf-8是为了防止中文乱码问题

你可能感兴趣的:(HttpClient访问restful)