HttpClient 的GET(带参数)、POST请求方式,工具类方法
来源:互联网 发布:淘宝如何申请退货退款 编辑:程序博客网 时间:2024/05/22 11:08
/** * 连接/断开操作 post方式 * @param url * @param json */private boolean connOrDisconnOperator(String url,String json){ CloseableHttpClient client = null; CloseableHttpResponse response = null; boolean flag = false; try{ HttpPost httpPost = new HttpPost(url); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(50000) .setSocketTimeout(50000) .setConnectionRequestTimeout(120000).build(); httpPost.setConfig(requestConfig); List<BasicNameValuePair> list = Lists.newArrayListWithExpectedSize(1); list.add(new BasicNameValuePair("json",json)); httpPost.setEntity(new UrlEncodedFormEntity(list)); client = HttpClients.createDefault(); response = client.execute(httpPost); if(response.getStatusLine().getStatusCode() == 200){ InputStream is = response.getEntity().getContent(); Map<String,Object> m = new ObjectMapper().readValue(StringUtil.getString(is),Map.class); String strState = m.get("state").toString(); if("SUCCESS".equals(strState)){ flag = true; } }else{ log.error(this.getClass(), "connOrDisconnOperator method fail:" + response.getStatusLine().getStatusCode()); } }catch (Exception ex){ log.error(this.getClass(), "connOrDisconnOperator method error",ex); }finally { if(response != null){ try { response.close(); } catch (IOException ex) { log.error(this.getClass(), "close response method error", ex); } } if(client != null){ try { client.close(); } catch (IOException ex) { log.error(this.getClass(), "close client method", ex); } } } return flag;}// get方式public Optional<Map<String,Object>> connOrDisconnOperator(String atisAirPortCode, String url) { CloseableHttpClient client = null; CloseableHttpResponse response = null; try{ HttpGet httpGet = new HttpGet(url + "?airportCode=" +atisAirPortCode); RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(5000) .setConnectTimeout(5000) .setConnectionRequestTimeout(120000).build(); httpGet.setConfig(requestConfig); client = HttpClients.createDefault(); response = client.execute(httpGet); if(response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 500){ Map<String,Object> map = Maps.newHashMapWithExpectedSize(4); InputStream is = response.getEntity().getContent(); JsonNode jn = new ObjectMapper().readTree(StringUtil.getString(is)); // 解析返回结果 String result = jn.get("state").asText(); if (result.equals("SUCCESS")) { System.out.println("-----------------------------成功"); } else { System.out.println("-----------------------------失败"); } return Optional.of(map); }else{ return Optional.absent(); } }catch (Exception ex) { log.error(this.getClass(), "getOSInfo() error", ex); return Optional.absent(); }finally { if(response != null){ try { response.close(); } catch (IOException ex) { log.error(this.getClass(), "close IO error", ex); } } if(client != null){ try { client.close(); } catch (IOException ex) { log.error(this.getClass(),"close IO error",ex); } } }}
阅读全文
0 0
- HttpClient 的GET(带参数)、POST请求方式,工具类方法
- HttpClient post 和get 带参数请求
- 用HttpClient发送get,post请求的工具类
- Apache 大三方法HTTP请求链接响应之HttpClient的GET和POST工具类封装
- HTTPClient get post 请求方式
- HttpClient Post、get 请求方式
- HttpClient Post、get 请求方式
- get方式带参数请求方法
- HttpClient的Post和Get各种方法的工具类
- android HttpClient get请求与post请求工具类
- HttpClient用Post和Get带参数提交帮助类
- HttpClient封装POST,GET方式HTTP请求
- java http请求的get和post方法工具类
- Httpclient的Get请求和Post请求
- HttpClient的Get请求和Post请求
- Java 通过HttpClient 带证书方式请求Https协议网站 (Post方式带xml文档,Get以及Delete方式)
- SIM900A 实现HTTP GET,带参数的POST请求
- 利用HttpClient的POST方式发起带参数的请求时,点击注册按钮无反应状态分析
- liunx下安装jdk8并配置环境变量
- 2.Python
- virtualbox win10 共享文件夹
- 基于JavaMail的Java邮件发送:复杂邮件发送
- 云星数据---Scala实战系列(精品版)】:Scala入门教程045-Scala实战源码-Scala Map操作
- HttpClient 的GET(带参数)、POST请求方式,工具类方法
- http协议 笔记
- 关于JavaScript的执行顺序,如果多个js中(a.js,b.js,c.js)都有一个方法(如test方法),那么如果再html中触发执行该test方法,是执行的那个js中的test方法(a.)?
- 解决加解密illegal key size问题
- Maven----公司常用以及IDEA集成Maven
- Google Data Binding Library 谷歌官方数据绑定库(七)属性Setter
- YII框架中自带的表单,搜索,分页代码的实现
- yum之学习路线
- Oracle数据库中Mybatis批量插入数据