`
WisdomTalk
  • 浏览: 57353 次
社区版块
存档分类
最新评论

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

    博客分类:
  • java
阅读更多
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
分享到:
评论
1 楼 chjy1983 2014-10-09  
请教下,struts2 action中怎么获取传的参数JSONObject

相关推荐

Global site tag (gtag.js) - Google Analytics