zoukankan      html  css  js  c++  java
  • JAVA 跨域请求 不用JSONP 不用CORS

    @RequestMapping(value = "/crossDomain")
    @ResponseBody
    public Object crossDomain(ServletRequest request, ServletResponse response) {
    logger.info(ZhongYiYaoAreaController.class + "跨域请求data开始");
    PageData pd = this.getPageData();
    String entityStr = "";
    PageData result = null;
    try {
    // 创建httpClient 对象
    HttpClient httpClient = new DefaultHttpClient();
    // 根据url 创建请求对象
    HttpPost httpPost = new HttpPost(pd.getString("url"));
    // 创建参数对象 传入请求对象中
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    for (Object key : pd.keySet()) {
    params.add(new BasicNameValuePair(key.toString(), pd.get(key).toString()));
    }
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "utf-8");
    httpPost.setEntity(entity);
    // 发出请求得到响应
    HttpResponse httpResponse = httpClient.execute(httpPost);
    // 接收到响应中的数据体 转化成json字符串
    entityStr = EntityUtils.toString(httpResponse.getEntity());
    result = new PageData();
    Map jsonMap = null;
    ArrayList jsonList = null;
    if (StringUtils.isBlank(entityStr)) {
    logger.error(ZhongYiYaoAreaController.class + "跨域请求异常jsonStr为空");
    throw new Exception("jsonStr为空");
    }
    // 根据objectMapper 通过反射 将jsonStr转换成Map
    ObjectMapper objectMapper = new ObjectMapper();
    if(pd.getString("url").indexOf("queryHisHerbsInfo") !=-1){
    jsonList = objectMapper.readValue(entityStr, ArrayList.class);
    if(jsonList.size()>0){
    result.put("data", jsonList.get(0));
    result.put("error", null);
    result.put("success", true);
    }else{
    result.put("data", new ArrayList());
    result.put("error", "跨域请求发生异常");
    result.put("success", false);
    }
    }else{
    jsonMap = objectMapper.readValue(entityStr, Map.class);
    if(jsonMap.get("error")==null || jsonMap.get("error")=="null"){
    result.put("data", jsonMap.get("data"));
    result.put("error", jsonMap.get("error"));
    result.put("success", jsonMap.get("success"));
    }else{
    result.put("data", new ArrayList());
    result.put("error", jsonMap.get("error"));
    result.put("success", false);
    }
    }

    } catch (Exception e) {
    result.put("data", new ArrayList());
    result.put("error", "跨域请求发生异常");
    result.put("success", false);
    logger.error(ZhongYiYaoAreaController.class + "跨域请求异常");
    logger.error(e.getMessage());
    return AppUtil.returnObject(new PageData(), result);
    } finally {
    logger.info(ZhongYiYaoAreaController.class + "跨域请求结束");
    }
    return AppUtil.returnObject(new PageData(), result);
    }
  • 相关阅读:
    06-图3 六度空间 (30 分)
    06-图2 Saving James Bond
    06-图1 列出连通集 (25 分)
    05-树9 Huffman Codes (30 分)
    05-树8 File Transfer (25 分)
    05-树7 堆中的路径 (25 分)
    04-树6 Complete Binary Search Tree (30 分)
    04-树5 Root of AVL Tree (25 分)
    03-树3 Tree Traversals Again (25 分)
    只允许输入数字的TextBox控件
  • 原文地址:https://www.cnblogs.com/g-yang/p/9548094.html
Copyright © 2011-2022 走看看