首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

httpclient发送Json请求,结果回来Json

2012-09-14 
httpclient发送Json请求,结果返回Json.public static JSONObject post(String url,JSONObject json){HttpC

httpclient发送Json请求,结果返回Json.

public static JSONObject post(String url,JSONObject json){HttpClient client = new DefaultHttpClient();HttpPost post = new HttpPost(url);JSONObject response = null;try {StringEntity s = new StringEntity(json.toString());s.setContentEncoding("UTF-8");s.setContentType("application/json");post.setEntity(s);HttpResponse res = client.execute(post);if(res.getStatusLine().getStatusCode() == HttpStatus.OK.value()){HttpEntity entity = res.getEntity();String charset = EntityUtils.getContentCharSet(entity);response = new JSONObject(new JSONTokener(new InputStreamReader(entity.getContent(),charset)));}} catch (Exception e) {throw new RuntimeException(e);}return response;}

用到的jar包:httpclient-4.1.1.jar以及辅助类
json-lib-2.4-jdk15.jar

热点排行