Httpclient 发送 get 请求

骑猪看日落 2022-12-13 14:03 350阅读 0赞

Httpclient 发送get请求:

1、工具客户端代码:

  1. public NmpResponse doGet(NmpRequest request) throws IOException {
  2. logger.info("------------开始GET请求 nmpHttpClient------------");
  3. String charset="UTF-8";
  4. HttpClient httpClient = new HttpClient();
  5. //设置Http连接超时为15秒
  6. httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
  7. Map<String,String> params = request.getParams();
  8. String paramValue ="";
  9. if (null != params && params.size()>0) {
  10. paramValue=paramValue+"?";
  11. for(Map.Entry<String,String> param : params.entrySet()) {
  12. String key = param.getKey();
  13. String paramEncode = null;
  14. if ("?".equals(paramValue)) {
  15. paramEncode = URLEncoder.encode(params.get(key),"UTF-8");//参数转义
  16. paramValue = paramValue+key+"="+ paramEncode;
  17. } else {
  18. paramEncode = URLEncoder.encode(params.get(key),"UTF-8");
  19. paramValue = paramValue+"&"+key+"="+ paramEncode;
  20. }
  21. }
  22. }
  23. String URL =NMP_ROOT_URL+request.getUrl()+paramValue;
  24. System.out.println("访问接口的全路径转义后:"+URL);
  25. GetMethod getMethod = new GetMethod(URL);
  26. getMethod = setHeaderAuthInfo(getMethod);
  27. NmpResponse response = new NmpResponse();
  28. try {
  29. logger.info("------------开始执行GET请求,url:{}------------",URL);
  30. int statusCode = httpClient.executeMethod(getMethod);
  31. logger.info("------------GET请求结束,code={}------------",statusCode);
  32. if (statusCode != HttpStatus.SC_OK){
  33. logger.error("请求出错code={}:{}" ,statusCode ,getMethod.getStatusLine());
  34. }
  35. Header[] headers = getMethod.getResponseHeaders();
  36. byte[] responseBody = getMethod.getResponseBody();
  37. String s = new String(responseBody, charset);
  38. response.setRespBody(responseBody);
  39. response.setRespHeaders(headers);
  40. response.setResponseStr(s);
  41. logger.info("------------结束GET请求 nmpHttpClient------------");
  42. } catch (IOException e){
  43. logger.error("请求出错IOException:{}" ,e);
  44. throw e;
  45. } finally {
  46. getMethod.releaseConnection();//.释放连接
  47. }
  48. return response;
  49. }

2、服务端接收参数的方法可以这样设置:

  1. @GetMapping(value = {"/query/getData"})
  2. public Object getQueryPartition( @RequestParam("parameter") String parameter) {
  3. String catName = "" ;
  4. String layName = "" ;
  5. String parName = "" ;
  6. Integer version = -1 ;
  7. String productVersion = Constant.ProductVersionLastest ;
  8. if (! "".equals(parameter)){
  9. JSONObject jsonObject = JSONObject.parseObject(parameter);
  10. layerName = jsonObject.getString("layName");
  11. partitionName = jsonObject.getString("parName");
  12. if (StringUtils.isEmpty(layName)){
  13. return new JSONObject();
  14. }
  15. if (StringUtils.isEmpty(parName)){
  16. return new JSONObject();
  17. }
  18. if(jsonObject.containsKey("catName")){
  19. catalogName = jsonObject.getString("catName");
  20. }
  21. if(jsonObject.containsKey("version")){
  22. version = Integer.valueOf(jsonObject.getString("version"));
  23. }
  24. if(jsonObject.containsKey("productVersion")){
  25. productVersion = jsonObject.getString("productVersion");
  26. }
  27. }
  28. //这里处理业务的代码省略
  29. return null;
  30. }

3、双端代码已贴,大家可以试一下,有不明白的地方可以留言。

发表评论

表情:
评论列表 (有 0 条评论,350人围观)

还没有评论,来说两句吧...

相关阅读