HttpClient PostMethod 传递json

记录下,备查。

 

/**
	 * 获取post请求响应
	 * @param url
	 * @param params
	 * @return
	 */
	public static String urlPostMethod(String url,String params) {
		HttpClient httpClient = new HttpClient();
		PostMethod method = new PostMethod(url);
		try {
			if(params != null && !params.trim().equals("")) {
				RequestEntity requestEntity = new StringRequestEntity(params,"text/xml","UTF-8");
				method.setRequestEntity(requestEntity);
			}
			method.releaseConnection();
			httpClient.executeMethod(method);
			String responses= method.getResponseBodyAsString();
			return responses;
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

 

调用:

 

String getURL = "http://localhost:8080/pr/rest/aggregation/getAll";
String params = "{everyPageNum:22,currentPage:1}";
String s = DataAggregationUtil.urlPostMethod(getURL, params);

System.out.println(s);

 

你可能感兴趣的:(httpclient)