httpClient调用解决中文乱码问题以及使用

httpClient调用解决中文乱码问题

1.请求方:

 @ResponseBody
 @RequestMapping(value = "/exportVehicleRelation", method = RequestMethod.POST)
 @ApiOperation(value = "车辆相关导出", notes = "车辆相关导出")
 public List<ExportVehicleRelationDto> exportVehicleList(@ModelAttribute VehicleListQuery vehicleListQuery) throws IOException {
            LoginInfoService loginSessionBean = this.loginInfoService;
            Map<String,String> params = new HashMap<>();
            params.put("bindType","2");
            params.put("pageNo", vehicleListQuery.getPageNo().toString());
            params.put("pageSize", vehicleListQuery.getPageSize() == 10 ? "500" : vehicleListQuery.getPageSize().toString());
            params.put("appIds", StringUtils.isEmpty(vehicleListQuery.getAppIds())?deliveryHomeVehicleAppId:vehicleListQuery.getAppIds().toString());
            params.put("vehicleNumber", vehicleListQuery.getVehicleNumber());
            params.put("vehicleType", vehicleListQuery.getVehicleType());
            params.put("vehicleProperty", vehicleListQuery.getVehicleProperty());
            params.put("vehicleOverallLength", vehicleListQuery.getVehicleOverallLength());
            params.put("organization", vehicleListQuery.getOrganization());
            params.put("partyId", loginSessionBean==null?"":loginSessionBean.getMerchantId().toString());
            String s = new ClientUtils().doPostMethod("http://mt-magicCube-vip:8080/magicCubeService/dHome/vehicle/exportVehicleList", params, "UTF-8");
            List<ExportVehicleRelationDto> exportVehicleRelationDtos = JSONArray.parseArray(s,ExportVehicleRelationDto.class);
            return exportVehicleRelationDtos;
 }

下面这个方法解决了将String的json串转换成List集合,使用的是阿里的fastjson转换工具:

List<ExportVehicleRelationDto> exportVehicleRelationDtos = JSONArray.parseArray(s,ExportVehicleRelationDto.class);

2.工具类new ClientUtils().doPostMethod:

package tf56.magiccubeapi.util.excel;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ClientUtils {
 
	/**
    * POST             请求
    * @param url       请求地址
    * @param params    请求参数
    * @param encode    编码格式
    * @return
    * @throws ClientProtocolException
    * @throws IOException
    */
   public String doPostMethod(String url, Map<String,String> params, String encode) throws ClientProtocolException, IOException {
       HttpClient httpclient = HttpClients.createDefault();
       HttpPost httpPost = new HttpPost(url);
       if (params != null) {
           List<NameValuePair> form = new ArrayList<NameValuePair>();
           for (String name : params.keySet()) {
               form.add(new BasicNameValuePair(name,params.get(name)));
           }
           UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form,encode);
           httpPost.setEntity(entity);
           //设置请求头
           httpPost.setHeader("Accept","text/plain;charset=utf-8");
           httpPost.setHeader("Cache-Control","no-cache");
       }
       HttpResponse response = httpclient.execute(httpPost);
       String result = EntityUtils.toString(response.getEntity());
       return result;
   }
   
   /**
    * GET
    * @param url
    * @param params
    * @param encode
    * @return
    * @throws ClientProtocolException
    * @throws IOException
    */
   public JSONObject doGetMethod(String url,List<NameValuePair> params,String encode) throws ClientProtocolException, IOException{
	   HttpClient httpclient = HttpClients.createDefault();
	 //参数转换为字符串
       String paramsStr = EntityUtils.toString(new UrlEncodedFormEntity(params, encode));
       url = url + "?" + paramsStr;
       // 创建httpget.
       HttpGet httpget = new HttpGet(url);
       System.out.println("executing request " + httpget.getURI());
       HttpResponse response = httpclient.execute(httpget);
       String result = EntityUtils.toString(response.getEntity());
       JSONObject obj = JSONObject.parseObject(result);
	   return obj;
   }
   
}

这个工具类提供了GET和POST请求而我的项目中使用了POST请求
//设置请求头

httpPost.setHeader("Accept","text/plain;charset=utf-8");

上面方法解决了

HttpResponse response = httpclient.execute(httpget);

response响应回来的编码ISO8859-1乱码问题;
3.被调用方:

    @ResponseBody
    @RequestMapping(value = "/exportVehicleList", method = RequestMethod.POST)
    public String exportVehicleList(HttpServletRequest request){
        String bindType = request.getParameter("bindType");
        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");
        String appIds = request.getParameter("appIds");
        String replace = appIds.replace("[", "").replace("]", "").replaceAll(" ","");
        String[] split = StringUtils.split(replace, ",");
        //将应用appIds转换为list集合
        List<String> strings = new ArrayList<>();
        for (String s : split) {
            strings.add(s);
        }
        String vehicleNumber = request.getParameter("vehicleNumber");
        String vehicleType = request.getParameter("vehicleType");
        String vehicleProperty = request.getParameter("vehicleProperty");
        String vehicleOverallLength = request.getParameter("vehicleOverallLength");
        String partyId = request.getParameter("partyId");
        String organization = request.getParameter("organization");
        PageModel<ExportVehicleRelationDto> exportVehicleRelationDtoPageModel = null;
        VehicleListQuery vehicleListQuery = new VehicleListQuery();
        vehicleListQuery.setBindType(bindType);
        vehicleListQuery.setPageNo(Integer.valueOf(pageNo));
        vehicleListQuery.setPageSize(Integer.valueOf(pageSize));
        vehicleListQuery.setAppIds(strings);
        vehicleListQuery.setVehicleNumber(vehicleNumber);
        vehicleListQuery.setVehicleType(vehicleType);
        vehicleListQuery.setVehicleProperty(vehicleProperty);
        vehicleListQuery.setVehicleOverallLength(vehicleOverallLength);
        if(StringUtils.isNotBlank(partyId)){
            vehicleListQuery.setPartyId(Integer.valueOf(partyId));
        }
        vehicleListQuery.setOrganization(organization);
        try {
            exportVehicleRelationDtoPageModel = magicCubeVehicleService.queryMagicCubeVehicleByCode(vehicleListQuery);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String toJSONString = JSON.toJSONString(exportVehicleRelationDtoPageModel.getDatas());
        return toJSONString;
    }

我们使用httpClient调用方式主要是解决dubbo微服务调用数据量小的问题,而dubbo是henssion协议默认数据量是100k大小,满足不了我们的业务需求。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值