【taotao】HttpClient

来源:互联网 发布:ssd优化 编辑:程序博客网 时间:2024/06/06 10:02
上篇博客中,我们了解到了跨域,并且学会了使用jsonp的方案解决跨域的问题。但频繁的跨域请求,必然
会使得频繁地使用ajax请求数据,而做网站并不推荐大面积使用ajax。
那么,不使用ajax,我们还可以如何获取数据呢?
【方案】
在负责页面处理的portal项目中,调用负责业务逻辑的rest项目发布的服务,获得数据,将数据传递给
jsp。要调用服务需要使用java代码模拟浏览器调用服务请求数据。
因此可以使用HttpClient。
【HttpClient】
1. 使用步骤
第一步:把HttpClient使用的jar包添加到工程中。
第二步:创建一个HttpClient的测试类
第三步:创建测试方法。
第四步:创建一个HttpClient对象
第五步:创建一个HttpGet对象,需要制定一个请求的url
第六步:执行请求。
第七步:接收返回结果。HttpEntity对象。
第八步:取响应的内容
第九步:关闭HttpGet、HttpClient。
2. Get请求测试代码

public void testHttpGet() throws Exception {// 第一步:把HttpClient使用的jar包添加到工程中。// 第二步:创建一个HttpClient的测试类// 第三步:创建测试方法。// 第四步:创建一个HttpClient对象CloseableHttpClient httpClient = HttpClients.createDefault();// 第五步:创建一个HttpGet对象,需要制定一个请求的urlHttpGet get = new HttpGet("http://www.itheima.com");// 第六步:执行请求。CloseableHttpResponse response = httpClient.execute(get);// 第七步:接收返回结果。HttpEntity对象。HttpEntity entity = response.getEntity();// 第八步:取响应的内容。String html = EntityUtils.toString(entity);System.out.println(html);// 第九步:关闭response、HttpClient。response.close();httpClient.close();}

3.HttpClient工具类代码:

public class HttpClientUtil {public static String doGet(String url, Map<String, String> param) {// 创建Httpclient对象CloseableHttpClient httpclient = HttpClients.createDefault();String resultString = "";CloseableHttpResponse response = null;try {// 创建uriURIBuilder builder = new URIBuilder(url);if (param != null) {for (String key : param.keySet()) {builder.addParameter(key, param.get(key));}}URI uri = builder.build();// 创建http GET请求HttpGet httpGet = new HttpGet(uri);// 执行请求response = httpclient.execute(httpGet);// 判断返回状态是否为200if (response.getStatusLine().getStatusCode() == 200) {resultString = EntityUtils.toString(response.getEntity(), "UTF-8");}} catch (Exception e) {e.printStackTrace();} finally {try {if (response != null) {response.close();}httpclient.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}public static String doGet(String url) {return doGet(url, null);}public static String doPost(String url, Map<String, String> param) {// 创建Httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = null;String resultString = "";try {// 创建Http Post请求HttpPost httpPost = new HttpPost(url);// 创建参数列表if (param != null) {List<NameValuePair> paramList = new ArrayList<>();for (String key : param.keySet()) {paramList.add(new BasicNameValuePair(key, param.get(key)));}// 模拟表单UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);httpPost.setEntity(entity);}// 执行http请求response = httpClient.execute(httpPost);resultString = EntityUtils.toString(response.getEntity(), "utf-8");} catch (Exception e) {e.printStackTrace();} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}public static String doPost(String url) {return doPost(url, null);}public static String doPostJson(String url, String json) {// 创建Httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = null;String resultString = "";try {// 创建Http Post请求HttpPost httpPost = new HttpPost(url);// 创建请求内容StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);httpPost.setEntity(entity);// 执行http请求response = httpClient.execute(httpPost);resultString = EntityUtils.toString(response.getEntity(), "utf-8");} catch (Exception e) {e.printStackTrace();} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}}

4. HttpClient项目应用

//调用其它项目服务获得数据
String json = HttpClientUtil.doGet("http://localhost:8081/rest/content/89");
【总结】
接触的项目越来越多了,而不同的项目都会用到各种不同的框架,不同框架下的项目,各个子项目或各模块
间的调用方法也是不一样的。而在这个项目中,并没有加入其它分布式框架,所以,学习到了解决项目中互相调用
(跨域)的HttpClient解决方案,这应该也算是最基础的解决方案之一。


0 0
原创粉丝点击