博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Httpclient 完美解决服务端跨域问题
阅读量:7037 次
发布时间:2019-06-28

本文共 12588 字,大约阅读时间需要 41 分钟。

   项目需求:

     jsonp是从前台js的角度考虑,通过Ajax调用springMVC的接口。同一个ip、同一个网络协议、同一个端口,三者都满足就是同一个域,否则就是跨域问题了。首页广告需要一个轮播的效果,取后台数据json格式。上篇博客介绍了使用jsonp来解决跨域,现在有个新的方法来解决,那就是:ajax请求地址改为自己系统的后台地址,之后在自己的后台用HttpClient请求url。封装好的跨域请求url工具类。封装一个get一个POST即可。

    两者的区别就在于,jsonp是基于客户端的跨域解决。而httpclient是基于服务端的跨域解决。

    我现在有两个maven项目:

Taotao-portal(8082端口)

Taotao-rest(8081端口)

    要使用httpclient需要在maven中引用(portal):

[html]
  1. <!-- httpclient -->  
  2. <dependency>  
  3.     <groupId>org.apache.httpcomponents</groupId>  
  4.     <artifactId>httpclient</artifactId>  
  5. </dependency>  
org.apache.httpcomponents
httpclient

    rest项目中写了个后台的服务调广告的数据,在portal项目中的service层来调用rest项目中的controller层提供的服务。

httpclient工作图解:

核心代码展示:

(portal项目)contentcontroller.java

[java]
  1. @Controller  
  2. public class ContentController {  
  3.     @Autowired  
  4.     private ContentService contentService;  
  5.           
  6.     //getdata  
  7.     @RequestMapping("/content/{cid}")  
  8.     @ResponseBody  
  9.     public TaotaoResult getConentList(@PathVariable Long cid){  
  10.           
  11.         try {  
  12.             List<TbContent> list=contentService.getContentList(cid);  
  13.             return TaotaoResult.ok(list);  
  14.         } catch (Exception e) {  
  15.             e.printStackTrace();  
  16.             return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));  
  17.         }  
  18.     }  
  19. }  
@Controllerpublic class ContentController {    @Autowired    private ContentService contentService;            //getdata    @RequestMapping("/content/{cid}")    @ResponseBody    public TaotaoResult getConentList(@PathVariable Long cid){                try {            List
list=contentService.getContentList(cid); return TaotaoResult.ok(list); } catch (Exception e) { e.printStackTrace(); return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e)); } }}

(portal项目)HttpClientUtil.java

[java]
  1. package com.taotao.common.utils;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.URI;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. import org.apache.http.NameValuePair;  
  10. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  11. import org.apache.http.client.methods.CloseableHttpResponse;  
  12. import org.apache.http.client.methods.HttpGet;  
  13. import org.apache.http.client.methods.HttpPost;  
  14. import org.apache.http.client.utils.URIBuilder;  
  15. import org.apache.http.entity.ContentType;  
  16. import org.apache.http.entity.StringEntity;  
  17. import org.apache.http.impl.client.CloseableHttpClient;  
  18. import org.apache.http.impl.client.HttpClients;  
  19. import org.apache.http.message.BasicNameValuePair;  
  20. import org.apache.http.util.EntityUtils;  
  21.   
  22. public class HttpClientUtil {  
  23.   
  24.     public static String doGet(String url, Map<String, String> param) {  
  25.   
  26.         // 创建Httpclient对象  
  27.         CloseableHttpClient httpclient = HttpClients.createDefault();  
  28.   
  29.         String resultString = "";  
  30.         CloseableHttpResponse response = null;  
  31.         try {  
  32.             // 创建uri  
  33.             URIBuilder builder = new URIBuilder(url);  
  34.             if (param != null) {  
  35.                 for (String key : param.keySet()) {  
  36.                     builder.addParameter(key, param.get(key));  
  37.                 }  
  38.             }  
  39.             URI uri = builder.build();  
  40.   
  41.             // 创建http GET请求  
  42.             HttpGet httpGet = new HttpGet(uri);  
  43.   
  44.             // 执行请求  
  45.             response = httpclient.execute(httpGet);  
  46.             // 判断返回状态是否为200  
  47.             if (response.getStatusLine().getStatusCode() == 200) {  
  48.                 resultString = EntityUtils.toString(response.getEntity(), "UTF-8");  
  49.             }  
  50.         } catch (Exception e) {  
  51.             e.printStackTrace();  
  52.         } finally {  
  53.             try {  
  54.                 if (response != null) {  
  55.                     response.close();  
  56.                 }  
  57.                 httpclient.close();  
  58.             } catch (IOException e) {  
  59.                 e.printStackTrace();  
  60.             }  
  61.         }  
  62.         return resultString;  
  63.     }  
  64.   
  65.     public static String doGet(String url) {  
  66.         return doGet(url, null);  
  67.     }  
  68.   
  69.     public static String doPost(String url, Map<String, String> param) {  
  70.         // 创建Httpclient对象  
  71.         CloseableHttpClient httpClient = HttpClients.createDefault();  
  72.         CloseableHttpResponse response = null;  
  73.         String resultString = "";  
  74.         try {  
  75.             // 创建Http Post请求  
  76.             HttpPost httpPost = new HttpPost(url);  
  77.             // 创建参数列表  
  78.             if (param != null) {  
  79.                 List<NameValuePair> paramList = new ArrayList<>();  
  80.                 for (String key : param.keySet()) {  
  81.                     paramList.add(new BasicNameValuePair(key, param.get(key)));  
  82.                 }  
  83.                 // 模拟表单  
  84.                 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);  
  85.                 httpPost.setEntity(entity);  
  86.             }  
  87.             // 执行http请求  
  88.             response = httpClient.execute(httpPost);  
  89.             resultString = EntityUtils.toString(response.getEntity(), "utf-8");  
  90.         } catch (Exception e) {  
  91.             e.printStackTrace();  
  92.         } finally {  
  93.             try {  
  94.                 response.close();  
  95.             } catch (IOException e) {  
  96.                 // TODO Auto-generated catch block  
  97.                 e.printStackTrace();  
  98.             }  
  99.         }  
  100.   
  101.         return resultString;  
  102.     }  
  103.   
  104.     public static String doPost(String url) {  
  105.         return doPost(url, null);  
  106.     }  
  107.       
  108.     public static String doPostJson(String url, String json) {  
  109.         // 创建Httpclient对象  
  110.         CloseableHttpClient httpClient = HttpClients.createDefault();  
  111.         CloseableHttpResponse response = null;  
  112.         String resultString = "";  
  113.         try {  
  114.             // 创建Http Post请求  
  115.             HttpPost httpPost = new HttpPost(url);  
  116.             // 创建请求内容  
  117.             StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);  
  118.             httpPost.setEntity(entity);  
  119.             // 执行http请求  
  120.             response = httpClient.execute(httpPost);  
  121.             resultString = EntityUtils.toString(response.getEntity(), "utf-8");  
  122.         } catch (Exception e) {  
  123.             e.printStackTrace();  
  124.         } finally {  
  125.             try {  
  126.                 response.close();  
  127.             } catch (IOException e) {  
  128.                 // TODO Auto-generated catch block  
  129.                 e.printStackTrace();  
  130.             }  
  131.         }  
  132.   
  133.         return resultString;  
  134.     }  
  135. }  
package com.taotao.common.utils;import java.io.IOException;import java.net.URI;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.client.utils.URIBuilder;import org.apache.http.entity.ContentType;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;public class HttpClientUtil {    public static String doGet(String url, Map
param) { // 创建Httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); String resultString = ""; CloseableHttpResponse response = null; try { // 创建uri URIBuilder 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); // 判断返回状态是否为200 if (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
param) { // 创建Httpclient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String resultString = ""; try { // 创建Http Post请求 HttpPost httpPost = new HttpPost(url); // 创建参数列表 if (param != null) { List
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) { // TODO Auto-generated catch block 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) { // TODO Auto-generated catch block e.printStackTrace(); } } return resultString; }}
(rest项目)contentserviceimpl.java

[java]
  1. @Service  
  2. public class ContentServiceImpl implements ContentService {  
  3.   
  4.     //service 写活,读配置文件  
  5.     @Value("${REST_BASE_URL}")  
  6.     private String REST_BASE_URL;  
  7.     @Value("${REST_CONTENT_URL}")  
  8.     private String REST_CONTENT_URL;  
  9.     @Value("${REST_CONTENT_AD1_CID}")  
  10.     private String REST_CONTENT_AD1_CID;  
  11.     @Override  
  12.     public String getAd1List() {  
  13.         //调用服务获得数据  跨域请求:http://localhost:8081/content/89  
  14.         String json = HttpClientUtil.doGet(REST_BASE_URL + REST_CONTENT_URL + REST_CONTENT_AD1_CID);  
  15.         //把json转换成java对象  
  16.         TaotaoResult taotaoResult = TaotaoResult.formatToList(json, TbContent.class);  
  17.         //取data属性,内容列表  
  18.         List<TbContent> contentList = (List<TbContent>) taotaoResult.getData();  
  19.         //把内容列表转换成AdNode列表  
  20.         List<AdNode> resultList = new ArrayList<>();  
  21.         for (TbContent tbContent : contentList) {  
  22.             AdNode node = new AdNode();  
  23.             node.setHeight(240);  
  24.             node.setWidth(670);  
  25.             node.setSrc(tbContent.getPic());  
  26.               
  27.             node.setHeightB(240);  
  28.             node.setWidthB(550);  
  29.             node.setSrcB(tbContent.getPic2());  
  30.               
  31.             node.setAlt(tbContent.getSubTitle());  
  32.             node.setHref(tbContent.getUrl());  
  33.               
  34.             resultList.add(node);  
  35.         }  
  36.         //需要把resultList转换成json数据  
  37.         String resultJson = JsonUtils.objectToJson(resultList);  
  38.         return resultJson;  
  39.     }  
  40. }  
@Servicepublic class ContentServiceImpl implements ContentService {    //service 写活,读配置文件    @Value("${REST_BASE_URL}")    private String REST_BASE_URL;    @Value("${REST_CONTENT_URL}")    private String REST_CONTENT_URL;    @Value("${REST_CONTENT_AD1_CID}")    private String REST_CONTENT_AD1_CID;    @Override    public String getAd1List() {        //调用服务获得数据  跨域请求:http://localhost:8081/content/89        String json = HttpClientUtil.doGet(REST_BASE_URL + REST_CONTENT_URL + REST_CONTENT_AD1_CID);        //把json转换成java对象        TaotaoResult taotaoResult = TaotaoResult.formatToList(json, TbContent.class);        //取data属性,内容列表        List
contentList = (List
) taotaoResult.getData(); //把内容列表转换成AdNode列表 List
resultList = new ArrayList<>(); for (TbContent tbContent : contentList) { AdNode node = new AdNode(); node.setHeight(240); node.setWidth(670); node.setSrc(tbContent.getPic()); node.setHeightB(240); node.setWidthB(550); node.setSrcB(tbContent.getPic2()); node.setAlt(tbContent.getSubTitle()); node.setHref(tbContent.getUrl()); resultList.add(node); } //需要把resultList转换成json数据 String resultJson = JsonUtils.objectToJson(resultList); return resultJson; }}
(rest项目)indexcontroller

[java]
  1. @Autowired  
  2.     private ContentService contentService;  
  3.       
  4.     @RequestMapping("/index")  
  5.     public String showIndex(Model model){  
  6.         String json=contentService.getAd1List();  
  7.         model.addAttribute("ad1",json);  
  8.         return "index";  
  9.     }  
@Autowired    private ContentService contentService;        @RequestMapping("/index")    public String showIndex(Model model){        String json=contentService.getAd1List();        model.addAttribute("ad1",json);        return "index";    }

查看网页源代码,可以看到传过来的json格式的数据。

总结:

      HttpClient与Jsonp能够轻易的解决跨域问题,从而得到自己想要的数据(来自不同IP,协议,端口),唯一的不同点是,HttpClient是在Java代码中进行跨域访问,而Jsonp是在js中进行跨域访问。跨域还有一级跨域,二级跨域,更多内容值得研究。

你可能感兴趣的文章
蒙特罗卡π算法(C++语言描述)
查看>>
数据库服务器 之 PostgreSQL的配置文件及用户权限
查看>>
自动生成单据号
查看>>
使用Maven管理Eclipse Java项目
查看>>
Perforce useage
查看>>
C#学习笔记—对话框的初始化
查看>>
MVC扩展DataAnnotationsModelMetadataProvider给model属性对应的页面元素添加任意属性和值...
查看>>
Flask and uWSGI - unable to load app 0 (mountpoint='') (callable not found or import error)
查看>>
mormot中间件成功匹配客户端FDMemTable和ClientDataSet
查看>>
===
查看>>
IIS 加载 JSON 错误 404 解决办法
查看>>
BZOJ 1010 [HNOI2008]玩具装箱toy(单调队列优化DP)
查看>>
FZU 1977 Pandora adventure (插头DP)
查看>>
Ubuntu12.04-64bits搭建FFmpeg环境
查看>>
Qualcomm Android display架构分析
查看>>
艾伟也谈项目管理,有一种企业文化叫产品精神
查看>>
微软ASP.NET站点部署指南(3):使用Web.Config文件的Transformations
查看>>
Sharp-P(#P)和NP计算复杂度[转]
查看>>
Core Animation学习笔记
查看>>
VC创建定时关闭的MessageBox
查看>>