HttpClient通信

1.背景

大多数系统功能和代码都是自己写的,自己用,但是在有些情况下,我们可以利用已经存在的系统,完成对自己实现相对很麻烦的功能,这些一般代价相对较大,自己不可能专门写一个系统或者太过很复杂的代码来完成不会经常用的功能。这时候就可以利用httpClient通信,在获得允许的情况下,安全的获取别的系统的服务。例如利用别的邮件系统发邮件,查询手机号码归属之类的功能。

2.之前有比较老版本的httpClient,现在旧的已经不提供更新了,apache 根据功能新分解出httpClient和httpCore,maven:

<dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.1</version>
 </dependency>
  新版本的使用更复杂,但是却相对安全很多。

官网有新版的快速教程:http://hc.apache.org/httpcomponents-client-ga/

3.整个api很复杂,详细了解很需要耐心和时间,这里也不建议大家学的那么细,毕竟用的时候不会太多,也不会用的那么复杂,可以在需要的时候,不懂还可以查api和官网教程。

我也没有看的很深入,只是大致写了几个简单的demo,觉得深入学习很需要时间。这里是一个很基本的上手例子:

 1 package org.mj.commonsHttpClient;
 2
 3 import java.io.IOException;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6
 7 import org.apache.http.HttpEntity;
 8 import org.apache.http.NameValuePair;
 9 import org.apache.http.client.ClientProtocolException;
10 import org.apache.http.client.entity.UrlEncodedFormEntity;
11 import org.apache.http.client.fluent.Form;
12 import org.apache.http.client.fluent.Request;
13 import org.apache.http.client.methods.CloseableHttpResponse;
14 import org.apache.http.client.methods.HttpGet;
15 import org.apache.http.client.methods.HttpPost;
16 import org.apache.http.impl.client.CloseableHttpClient;
17 import org.apache.http.impl.client.HttpClients;
18 import org.apache.http.message.BasicNameValuePair;
19 import org.apache.http.util.EntityUtils;
20
21 /**
22  * @author jing.ming
23  * @version 创建时间:2015年11月26日 上午9:03:06
24  * 使用最新的httpClient4.5.x
25  */
26 public class HttpClientNewVersion {
27
28     public static void getMethodTry() throws IOException {
29         CloseableHttpClient httpclient = HttpClients.createDefault();
30         HttpGet httpGet = new HttpGet("http://httpbin.org/get");
31         CloseableHttpResponse response1 = httpclient.execute(httpGet);
32         // The underlying HTTP connection is still held by the response object
33         // to allow the response content to be streamed directly from the
34         // network socket.
35         // In order to ensure correct deallocation of system resources
36         // the user MUST call CloseableHttpResponse#close() from a finally
37         // clause.
38         // Please note that if response content is not fully consumed the
39         // underlying
40         // connection cannot be safely re-used and will be shut down and
41         // discarded
42         // by the connection manager.
43         try {
44             System.out.println(response1.getStatusLine());
45             HttpEntity entity1 = response1.getEntity();
46             // do something useful with the response body
47             // and ensure it is fully consumed
48             EntityUtils.consume(entity1);
49         } finally {
50             response1.close();
51         }
52     }
53
54     public static void postMethodTry() throws ClientProtocolException, IOException{
55         CloseableHttpClient httpclient = HttpClients.createDefault();
56         HttpPost httpPost = new HttpPost("http://httpbin.org/post");
57         List <NameValuePair> nvps = new ArrayList <NameValuePair>();
58         nvps.add(new BasicNameValuePair("username", "vip"));
59         nvps.add(new BasicNameValuePair("password", "secret"));
60         httpPost.setEntity(new UrlEncodedFormEntity(nvps));
61         CloseableHttpResponse response2 = httpclient.execute(httpPost);
62
63         try {
64             System.out.println(response2.getStatusLine());
65             HttpEntity entity2 = response2.getEntity();
66             // do something useful with the response body
67             // and ensure it is fully consumed
68             EntityUtils.consume(entity2);
69         } finally {
70             response2.close();
71         }
72     }
73
74     // fluent API ,更简单的处理方式
75     // The fluent API relieves the user from having to deal with manual
76     // deallocation of system
77     // resources at the cost of having to buffer response content in memory in
78     // some cases.
79     public static void fluentMode() throws ClientProtocolException, IOException {
80         Request.Get("http://targethost/homepage").execute().returnContent();
81         Request.Post("http://targethost/login")
82                 .bodyForm(Form.form().add("username", "vip").add("password", "secret").build())
83                 .execute().returnContent();
84     }
85
86     public static void main(String[] args) {
87         try {
88             getMethodTry();
89         } catch (ClientProtocolException e) {
90             e.printStackTrace();
91         } catch (IOException e) {
92             e.printStackTrace();
93         }
94     }
95
96 }
97  
时间: 2024-07-31 14:26:35

HttpClient通信的相关文章

详细讲解Android的网络通信(HttpUrlConnection和HttpClient)

前言,Android的网络通信的方式有两种:使用Socket或者HTTP,今天这一篇我们详细讲解使用HTTP实现的网络通信,HTTP又包括两种方式编程方式: (1)HttpUrlConnection: (2)HttpClient: 好了,我们直接进行讲解,当然之前也会有一部分有关Android网络通信的其他知识,我们也应该了解. 一.获取网络状态的方法 (1)MainActivity.java中的关键代码 1 2 3 4 5 6 7 8 //网络管理类,可以判断是否能上网,以及网络类型     

【转】Android开发笔记(序)写在前面的目录

原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经验教训,与网友互相切磋,从而去芜存菁进一步提升自己的水平.因此博主就想,入门的东西咱就不写了,人不能老停留在入门上:其次是想拾缺补漏,写写虽然小众却又用得着的东西:另外就是想以实用为主,不求大而全,但求小而精:还有就是有的知识点是java的,只是Android开发也会经常遇上,所以蛮记下来.个人的经

Http接口安全整理

1.Http接口安全概述: 1.1.Http接口是互联网各系统之间对接的重要方式之一,使用http接口,开发和调用都很方便,也是被大量采用的方式,它可以让不同系统之间实现数据的交换和共享,但由于http接口开放在互联网上,那么我们就需要有一定的安全措施来保证不能是随随便便就可以调用: 1.2.目前国内互联网公司主要采用两种做法实现接口的安全: 一种是以支付宝等支付公司为代表的私钥公钥签名验证机制; 一种是大量互联网企业都常采用的参数签名验证机制: 2. Http接口安全演进: 2.1.完全开放的

Android开发之网络请求通信专题(二):基于HttpClient的文件上传下载

上一篇专题Android开发之网络请求通信专题(一):基于HttpURLConnection的请求通信我们讲解了如何使用httpurlconnection来实现基本的文本数据传输.一般在实际开发中我们可以用于传输xml或者json格式的数据.今天我们来讲解另外一种http网络请求的方式:httpclient,并实现文件的上传和下载. 在这里插个题外话,其实这些网络请求有很多第三方jar包可以使用,这些包都封装得很好了.如果只是想使用,我们就直接拿别人得jar包来用就好.博主这里推荐一个叫xuti

重新想象 Windows 8.1 Store Apps (88) - 通信的新特性: 新的 HttpClient

[源码下载] 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之通信的新特性 新的 HttpClient http get string http get stream http post string http post stream 示例HTTP 服务端WebServer/HttpDemo.aspx.cs /* * 用于响应 http 请求 */ using System; using System.IO; using System.Threading; u

bugzilla4的xmlrpc接口api调用实现分享: xmlrpc + https + cookies + httpclient +bugzilla + java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能

xmlrpc .  https . cookies . httpclient.bugzilla . java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能,网上针对bugzilla的实现很少,针对xmlrpc的有但是基本都是http协议的,https下的认证处理比较麻烦,而且会话保持也是基本没有太多共享,所以本人决定结合xmlrpc\bugzilla官方文档,网友文章,结合个人经验总结而成,已经在window2007 64+jdk7位机器上调试通过 手把手教你如何实现: 第一步: 在

Android网络编程之使用HttpClient进行Get方式通信

在Android开发中,Android SDK附带了Apache的HttpClient,它是一个完善的客户端.它提供了对HTTP协议的全面支持,可以使用HttpClient的对象来执行HTTP GET和HTTP POST调用. HTTP工作原理: 1.客户端(一般是指浏览器,这里是指自己写的程序)与服务器建立连接 2.建立连接后,客户端向服务器发送请求 3.服务器接收到请求后,向客户端发送响应信息 4.客户端与服务器断开连接 HttpClient的一般使用步骤: 1.使用DefaultHttpC

android http 通信(httpclient 实现)

1.httpclient get 方式 HttpGet httpGet = new HttpGet(url); HttpClient client = new DefaultHttpClient(); HttpResponse response=client.execute(httpGet); if(response.getStatusLine().getStatusCode() == HttpStatus.SC_Ok){ String content = EntityUtils.toStrin

Android中使用HTTP和HttpClient进行通信

/** * 使用HTTP的Get方式进行数据请求 */ protected void httpGet() { /** * 进行异步请求 */ new AsyncTask<String, Void, Void>() { @Override protected Void doInBackground(String... params) { System.err.println("httpGet start"); // 在此方法中只能进行数据处理,不能与进行UI交互 try {