java之HttpClient 使用[get/post]
来源:互联网 发布:眼睛很亮女孩子知乎 编辑:程序博客网 时间:2024/06/10 03:00
在介绍 java 上的 网页的获取工具之前, 这里先介绍 linux 下自带的一个好用的网页获取工具 curl,有兴趣的可以去详细了解下。
HttpClient
HttpClient通过提供一个高效,最新和功能丰富的软件包来实现客户端的最新HTTP标准和建议
依赖
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version></dependency>
发送 get 请求并获取数据
public static BufferedReader httpGetMethod(String uri) throws IOException { // 创建客户端对象 CloseableHttpClient httpClient = HttpClients.createDefault(); // 跟据对象获取请求 -- get HttpGet get = new HttpGet(uri); // 接受响应对象 CloseableHttpResponse response = null; BufferedReader buffer = null; try{ response = httpClient.execute(get); String str = EntityUtils.toString(response.getEntity(), "utf-8"); buffer = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(str.getBytes("utf-8")))); return buffer; }catch (IOException e){ if(buffer != null){ buffer.close(); } return null; }finally { if(response != null){ response.close(); } httpClient.close(); }}
发送 post 请求
public static BufferedReader httpPostMethod(String uri, Map<String, String> map) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost post = new HttpPost(uri); CloseableHttpResponse response = null; BufferedReader buffer = null; if(map != null || map.size() != 0){ for(String key : map.keySet()){ post.setHeader(key, map.get(key)); } } try{ response = httpClient.execute(post); String str = EntityUtils.toString(response.getEntity(), "utf-8"); buffer = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(str.getBytes("utf-8")))); return buffer; }catch (IOException e){ if(buffer != null){ buffer.close(); } return null; }finally { if(response != null){ response.close(); } httpClient.close(); }}
使用post 发送json 数据:
public static BufferedReader httpPostMethod(String uri, Map<String, String> map, String data) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost post = new HttpPost(uri); CloseableHttpResponse response = null; BufferedReader buffer = null; String e; if(map != null || map.size() != 0) { Iterator entity = map.keySet().iterator(); while(entity.hasNext()) { e = (String)entity.next(); post.setHeader(e, (String)map.get(e)); } } StringEntity entity1 = new StringEntity(data); entity1.setContentType("application/json"); entity1.setContentEncoding("utf-8"); post.setEntity(entity1); BufferedReader var9; try { response = httpClient.execute(post); e = EntityUtils.toString(response.getEntity(), "utf-8"); buffer = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(e.getBytes("utf-8")))); var9 = buffer; return var9; } catch (IOException var13) { var13.printStackTrace(); if(buffer != null) { buffer.close(); } System.exit(-1); var9 = null; } finally { if(response != null) { response.close(); } httpClient.close(); } return var9; }
0 0
- java之HttpClient 使用[get/post]
- HttpClient get, post使用
- Java--Http: HttpClient(post/get)
- java使用httpclient模拟post请求和get请求示例
- Java使用HttpClient发送Get和Post请求
- java使用HttpClient模拟发送http的get、post请求
- java使用httpclient封装post请求和get的请求
- Java使用HttpClient发送Get请求和Post请求
- 【末世旅行之Java】HttpClient工具类,包含POST和GET请求,使用了连接池和代理
- HttpClient应用之GET和POST申请
- HttpClient使用Post和Get提交参数
- HttpClient使用Post和Get提交参数
- Post和Get在HttpClient的使用
- C#使用HttpClient进行GET&POST
- HttpClient使用Post和Get提交参数
- HttpClient使用get、post提交请求
- 使用Apache HttpClient发送get、post请求
- HttpClient GET POST DELETE方法使用
- Android Upload File to Server
- 集合总结[java]
- 设计模式-装饰者模式
- hdu 5794
- git 排错 fatal: 'git status --porcelain' failed in submodule abi/cpp
- java之HttpClient 使用[get/post]
- bootstra查阅参考文档
- Maven集成的Spring框架运行提示Failed to load ApplicationContext.xml
- SQL server安装跨语言失败咋整 求大神帮帮忙
- OpenGL(四)Shader错误 检测
- PS如何快速切图
- 带动画的点击可展开TextView
- xshell操作linux常用命令
- SVN解决不能查看最近提交日志的问题