用Httpclient通过post方式来实现http请求
来源:互联网 发布:spss数据分析软件下载 编辑:程序博客网 时间:2024/06/11 14:45
Http协议的重要性这里不多介绍,基于上一篇介绍Http请求的GET和POST方式,这篇继续用Httpclient通过post方式来实现Http请求。先简单介绍一下Httpclient。Httpclient相比传统JDK自带的URL Connection增加了易用性和灵活性,它支持Http协议,提供了功能丰富的工具包,不仅让客户端发送Http请求变得容易,而且也提高了开发人员的开发效率。
下面重点介绍一下使用方法,首先,pom文件
<!-- httpClient --><dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.6</version> </dependency><dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version></dependency><dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier></dependency><!-- json-lib还需要以下依赖包 --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency>具体代码如下:
public class HttpclientDemo {public List<DemoTypeEntity> getDemoList(String demoName, String demoStatus){List<String> resultList = new ArrayList<String>();CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse httpResponse = null;//URLString url = "http://192.168.0.1/httpclientDemo/service/getDemoList.do";//封装请求参数List<NameValuePair> params = Lists.newArrayList();params.add(new BasicNameValuePair("demoName", demoName));params.add(new BasicNameValuePair("demoStatus", demoStatus));try{//转换键值对String str = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));//创建请求--getHttpGet httpGet = new HttpGet(url+"?"+str);httpResponse = httpClient.execute(httpGet);//判断状态码if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){//解析结果String result = EntityUtils.toString(httpResponse.getEntity());JSONArray jsonArray = JSONArray.fromObject(result);resultList = JSONArray.toList(jsonArray, new DemoTypeEntity(), new JsonConfig());}//释放连接httpResponse.close();}catch(Exception e){e.printStackTrace();}return resultList;}}具体的调用过程如下:
1.创建CloseableHttpClient对象
2.设置请求参数
3.创建请求方法的实例,如果需要发送GET请求则创建HttpGet对象,如果需要发送POST请求则创建HttpPost对象,我们这里创建的是HttpGet对象
4.指定URL
5.设置头信息
6.调用CloseableHttpClient对象的execute方法,返回一个CloseableHttpResponse对象
解析结果时需要注意,通过调用CloseableHttpClient对象的execute方法,返回一个CloseableHttpResponse对象,返回的对象调用getEntity方法可获取HttpEntity对象,该对象包含了服务器的响应内容
1 0
- 用Httpclient通过post方式来实现http请求
- HttpClient封装POST,GET方式HTTP请求
- java 通过Httpclient模拟https Post 请求,实现双向认证
- HTTPClient get post 请求方式
- HttpClient Post、get 请求方式
- HttpClient Post、get 请求方式
- 用Httpclient来Post表单实现登录
- HttpClient Post请求方式请求数据
- Http请求之--C#的HttpWebRequest实现POST方式请求
- Android HTTP请求方式:HttpClient
- Android HTTP请求方式:HttpClient
- 使用HttpClient发送Post Http请求
- 使用HttpClient发送Post Http请求
- 使用HttpClient发送Post Http请求
- 使用HttpClient发送Post Http请求
- 使用HttpClient发送Post Http请求
- HttpClient中post请求http、https示例
- HttpClient中post请求http、https示例
- 在IDEA中编写mybatis,需要注意的问题
- HTTP请求之GET和POST
- Android应用开发性能优化完全分析
- Program Execution
- 素数的求法
- 用Httpclient通过post方式来实现http请求
- Javascript的类实现--------封装、抽象与继承
- js-escape()、encodeURI()、encodeURIComponent()区别
- QML 的 font 与 Canvas 的中 font
- logstash + grok 正则语法
- 第一篇博客(我的两年经历)
- HTTP详解(1)-工作原理
- 手写JSON解析框架
- tableView 简单使用