博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用httpClient发送get\post请求
阅读量:5243 次
发布时间:2019-06-14

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

maven依赖

1 
2
org.apache.httpcomponents
3
httpclient
4
4.5.2
5

GET请求:

1、参数直接拼接到URL后面,即http://test.com?a=1&b=2的形式

1 /** 2      * get请求,参数拼接在地址上 3      * @param url 请求地址加参数 4      * @return 响应 5      */ 6     public String get(String url) 7     { 8         String result = null; 9         CloseableHttpClient httpClient = HttpClients.createDefault();10         HttpGet get = new HttpGet(url);11         CloseableHttpResponse response = null;12         try {13             response = httpClient.execute(get);14             if(response != null && response.getStatusLine().getStatusCode() == 200)15             {16                 HttpEntity entity = response.getEntity();17                 result = entityToString(entity);18             }19             return result;20         } catch (IOException e) {21             e.printStackTrace();22         }finally {23             try {24                 httpClient.close();25                 if(response != null)26                 {27                     response.close();28                 }29             } catch (IOException e) {30                 e.printStackTrace();31             }32         }33         return null;34     }

2、参数放置到一个map中

1 /** 2      * get请求,参数放在map里 3      * @param url 请求地址 4      * @param map 参数map 5      * @return 响应 6      */ 7     public String getMap(String url,Map
map) 8 { 9 String result = null;10 CloseableHttpClient httpClient = HttpClients.createDefault();11 List
pairs = new ArrayList
();12 for(Map.Entry
entry : map.entrySet())13 {14 pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));15 }16 CloseableHttpResponse response = null;17 try {18 URIBuilder builder = new URIBuilder(url);19 builder.setParameters(pairs);20 HttpGet get = new HttpGet(builder.build());21 response = httpClient.execute(get);22 if(response != null && response.getStatusLine().getStatusCode() == 200)23 {24 HttpEntity entity = response.getEntity();25 result = entityToString(entity);26 }27 return result;28 } catch (URISyntaxException e) {29 e.printStackTrace();30 } catch (ClientProtocolException e) {31 e.printStackTrace();32 } catch (IOException e) {33 e.printStackTrace();34 }finally {35 try {36 httpClient.close();37 if(response != null)38 {39 response.close();40 }41 } catch (IOException e) {42 e.printStackTrace();43 }44 }45 46 return null;47 }

POST请求:

1、参数放到map中

1 /** 2      * 发送post请求,参数用map接收 3      * @param url 地址 4      * @param map 参数 5      * @return 返回值 6      */ 7     public String postMap(String url,Map
map) { 8 String result = null; 9 CloseableHttpClient httpClient = HttpClients.createDefault();10 HttpPost post = new HttpPost(url);11 List
pairs = new ArrayList
();12 for(Map.Entry
entry : map.entrySet())13 {14 pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));15 }16 CloseableHttpResponse response = null;17 try {18 post.setEntity(new UrlEncodedFormEntity(pairs,"UTF-8"));19 response = httpClient.execute(post);20 if(response != null && response.getStatusLine().getStatusCode() == 200)21 {22 HttpEntity entity = response.getEntity();23 result = entityToString(entity);24 }25 return result;26 } catch (UnsupportedEncodingException e) {27 e.printStackTrace();28 } catch (ClientProtocolException e) {29 e.printStackTrace();30 } catch (IOException e) {31 e.printStackTrace();32 }finally {33 try {34 httpClient.close();35 if(response != null)36 {37 response.close();38 }39 } catch (IOException e) {40 e.printStackTrace();41 }42 43 }44 return null;45 }

2、参数是json字符串

1 /** 2      * post请求,参数为json字符串 3      * @param url 请求地址 4      * @param jsonString json字符串 5      * @return 响应 6      */ 7     public String postJson(String url,String jsonString) 8     { 9         String result = null;10         CloseableHttpClient httpClient = HttpClients.createDefault();11         HttpPost post = new HttpPost(url);12         CloseableHttpResponse response = null;13         try {14             post.setEntity(new ByteArrayEntity(jsonString.getBytes("UTF-8")));15             response = httpClient.execute(post);16             if(response != null && response.getStatusLine().getStatusCode() == 200)17             {18                 HttpEntity entity = response.getEntity();19                 result = entityToString(entity);20             }21             return result;22         } catch (UnsupportedEncodingException e) {23             e.printStackTrace();24         } catch (ClientProtocolException e) {25             e.printStackTrace();26         } catch (IOException e) {27             e.printStackTrace();28         }finally {29             try {30                 httpClient.close();31                 if(response != null)32                 {33                     response.close();34                 }35             } catch (IOException e) {36                 e.printStackTrace();37             }38         }39         return null;40     }

entityToString方法:

1 private String entityToString(HttpEntity entity) throws IOException { 2         String result = null; 3         if(entity != null) 4         { 5             long lenth = entity.getContentLength(); 6             if(lenth != -1 && lenth < 2048) 7             { 8                 result = EntityUtils.toString(entity,"UTF-8"); 9             }else {10                 InputStreamReader reader1 = new InputStreamReader(entity.getContent(), "UTF-8");11                 CharArrayBuffer buffer = new CharArrayBuffer(2048);12                 char[] tmp = new char[1024];13                 int l;14                 while((l = reader1.read(tmp)) != -1) {15                     buffer.append(tmp, 0, l);16                 }17                 result = buffer.toString();18             }19         }20         return result;21     }

 

转载于:https://www.cnblogs.com/xiaosiyuan/p/6726668.html

你可能感兴趣的文章
按钮实现A标签新窗口打开(不用window.open)
查看>>
三维变换概述
查看>>
jmap命令
查看>>
第三次作业
查看>>
Python的classmethod和staticmethod区别
查看>>
Ubuntu12.04 英文环境下使用ibus输入中文并自动启动输入法
查看>>
SpringMVC 拦截器HandlerInterceptor(一)
查看>>
mvc知识应用
查看>>
数据结构之排序三:插入排序
查看>>
Class.forName(),classloader.loadclass用法详解
查看>>
团队任务3:每日立会(2018-10-25)
查看>>
创业记录0
查看>>
python pandas tensorflow使用总结
查看>>
UnityAssetExplorer + PowerVRTexTool
查看>>
jquery操作select取值,设置选中(转载)
查看>>
nodejs中间件拦截,express不登录无法进入后台页面
查看>>
c函数创建文件和路径
查看>>
PHP switch问题
查看>>
Github.com上有哪些比较有趣的PHP项目?
查看>>
SQL中Group By的使用详解
查看>>