HttpClient4.5 post
来源:互联网 发布:医疗网络咨询工资待遇 编辑:程序博客网 时间:2024/06/05 15:33
/** * POST请求 * @param url 请求地址 * @param data 请求数据 * @return 响应数据 * @throws Exception */public String post(String url, String data) throws Exception{HttpClient httpClient = null;HttpPost httpPost = null;HttpResponse response = null;String result = "";try {httpClient = HttpClients.createDefault();httpPost = new HttpPost(url);RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectionTimeout).setConnectTimeout(connectionTimeout).setSocketTimeout(connectionTimeout).build();httpPost.setConfig(requestConfig);StringEntity dataEntity = new StringEntity(data, Charset.forName("UTF-8"));httpPost.setEntity(dataEntity);response = httpClient.execute(httpPost);int retCode = response.getStatusLine().getStatusCode();if (200 == retCode){HttpEntity entity = response.getEntity();InputStream instream = entity.getContent();// 不超过2048M的内容可以转int total = (int) entity.getContentLength();byte[] bytes = new byte[total];int readCount = 0;int countTemp = 0;if ((countTemp = instream.read(bytes, readCount, total)) != -1) {readCount += countTemp;}result = new String(bytes, "UTF-8");} else {throw new Exception("请求失败,状态码为[" + retCode + "]");}} catch (Exception e) {throw e;} finally {if (httpPost != null) {httpPost.releaseConnection();}}return result;}
maven文件
<groupId>demoapi</groupId><artifactId>demoapi</artifactId><version>1.0</version><packaging>jar</packaging><name>demoapi</name><url>http://maven.apache.org</url><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- 基于httpcore的一套客户端 --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.2</version></dependency><!-- http协议实现包 --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.4.5</version></dependency><!-- 包含一些通用的编码解码算法。包括一些语音编码器,Hex,Base64 --><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.10</version></dependency><!-- 通用日志接口 --><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.21</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.21</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><!-- 用于测试 --><dependency><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId><version>2.3.0</version></dependency></dependencies><build><finalName>demoapi</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.6.0</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build>
0 0
- HttpClient4.5 post
- HttpClient4.5发送post请求
- httpclient4.5的get和post方法
- HttpClient4.5 post请求xml到服务器
- HttpClient4 Post XML数据
- HttpClient4 Post XML数据
- Httpclient4.3.5简单get,post实用(httpcomponents-client-4.3.5)
- httpClient4.5创建连接池进行post请求
- HttpClient4 POST数据及问题
- httpclient4 post请求传参数
- Httpclient4.5
- httpclient4.5
- HttpClient4.5
- httpClient4.5
- HttpClient4 Post XML到一个服务器上
- HttpClient4 post上传文件, struts2 action接收
- HttpClient4 post上传文件, struts2 action接收
- HttpClient4 Post XML到一个服务器上
- Android Topeka介绍
- 作用域和上下文、this关键字
- Android进程间通信之使用Messenger
- 如何发布一个war网站
- Struts标签、ongl表达式与值栈
- HttpClient4.5 post
- 再识Intent-实现调用Android内置浏览器打开网页
- slick log日志配置文件
- PLSQL 基础教程 二 增删改(DML)
- my97DatePicker的基本使用
- 【CodeVS】1083 Cantor表
- Vi/Vim键盘图
- linux 查看内存情况总结
- 3D Convolutional Neural Networks for Human Action Recognition