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,觉得深入学习很需要时间。这里是一个很基本的上手例子:

 package org.mj.commonsHttpClient;  

 import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.fluent.Form;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; /**
* @author jing.ming
* @version 创建时间:2015年11月26日 上午9:03:06
* 使用最新的httpClient4.5.x
*/
public class HttpClientNewVersion { public static void getMethodTry() throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://httpbin.org/get");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
// The underlying HTTP connection is still held by the response object
// to allow the response content to be streamed directly from the
// network socket.
// In order to ensure correct deallocation of system resources
// the user MUST call CloseableHttpResponse#close() from a finally
// clause.
// Please note that if response content is not fully consumed the
// underlying
// connection cannot be safely re-used and will be shut down and
// discarded
// by the connection manager.
try {
System.out.println(response1.getStatusLine());
HttpEntity entity1 = response1.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity1);
} finally {
response1.close();
}
} public static void postMethodTry() throws ClientProtocolException, IOException{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://httpbin.org/post");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", "vip"));
nvps.add(new BasicNameValuePair("password", "secret"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost); try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity2);
} finally {
response2.close();
}
} // fluent API ,更简单的处理方式
// The fluent API relieves the user from having to deal with manual
// deallocation of system
// resources at the cost of having to buffer response content in memory in
// some cases.
public static void fluentMode() throws ClientProtocolException, IOException {
Request.Get("http://targethost/homepage").execute().returnContent();
Request.Post("http://targethost/login")
.bodyForm(Form.form().add("username", "vip").add("password", "secret").build())
.execute().returnContent();
} public static void main(String[] args) {
try {
getMethodTry();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }

HttpClient通信的更多相关文章

  1. HttpClient 通信工具类

    package com.taotao.web.service; import java.util.ArrayList; import java.util.List; import java.util. ...

  2. Android 实现http通信(servlet做服务器端) HttpClient、HttpURLConnection实现登录验证

    一,写好服务器端 在eclipse或其它javaee开发工具中新建一个web项目(我这里的项目名是:Android),建一个servlet(我这里的servlet名是:LoginServlet),模拟 ...

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

    前言,Android的网络通信的方式有两种:使用Socket或者HTTP,今天这一篇我们详细讲解使用HTTP实现的网络通信,HTTP又包括两种方式编程方式: (1)HttpUrlConnection: ...

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

    原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...

  5. Http接口安全整理

    1.Http接口安全概述: 1.1.Http接口是互联网各系统之间对接的重要方式之一,使用http接口,开发和调用都很方便,也是被大量采用的方式,它可以让不同系统之间实现数据的交换和共享,但由于htt ...

  6. Android的网络编程

    1.3主要接口 Android平台有三种网络接口可以使用,他们分别是:java.net.*(标准Java接口).Org.apache接口和Android.net.*(Android网络接口).下面分别 ...

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

    xmlrpc .  https . cookies . httpclient.bugzilla . java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能,网上针对bugzilla的实现很 ...

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

    [源码下载] 重新想象 Windows 8.1 Store Apps (88) - 通信的新特性: 新的 HttpClient 作者:webabcd 介绍重新想象 Windows 8.1 Store ...

  9. HttpClient实现客户端与服务器的通信

    本篇主要讲解了利用HttpClient实现 windows主机与linux服务器的通信与传递数据 HttpClient代码,服务器端配置 系统和安装软件 1)ubuntu 14.04 64位系统 2) ...

随机推荐

  1. phpstorm 右下角显示updating indices,一直有任务卡着

    其实就是生成的这个node_modules目录内文件太多了,选中node_modules这个目录右键,选择Excluded 一直在加载忽略掉这个文件就可以了

  2. 【三小时学会Kubernetes!(一) 】容器简介及为每个服务创建镜像

    容器是什么 Kubernetes 是容器管理平台.可想而知我们需要容器去管理它们.但是容器是什么?Docker 官方文档的最佳答案如下: 容器映像是轻量级的.独立的.可执行软件包,包含所有可运行的东西 ...

  3. 最简js深浅拷贝说明

    1.浅拷贝 浅拷贝是拷贝引用,拷贝后的引用都是指向同一个对象的实例,彼此之间的操作会互相影响.  浅拷贝分两种情况: 1.直接拷贝源对象的引用 2. 源对象拷贝实例,但其属性对象(类型为Object, ...

  4. Eclipse中快速定位

    Eclipse中快速定位 选中项目,ctrl+h 一.目标 查找如下的页面属于哪个activity 二.步骤 1.查找关键字 上述页面中“点我”两个字比较显眼,我们可以去android项目中搜索出现“ ...

  5. JMeter中响应数据显示乱码问题解决

    方法一.UTF-8 路径:JMeter-->bin-->jmeter.properties 打开之后 #sampleresult.default.encoding=ISO-8859-1 改 ...

  6. python PIL/Pillow图像扩展、复制、粘贴处理

    http://blog.csdn.net/yuanyangsdo/article/details/60957685

  7. zsh 安装powerline 主题特效

    查看当前使用的shell脚本是哪一种   echo $0 1. 安装Powerline   使用pip指令,安装方法:   pip install powerline-status   如果没有,则先 ...

  8. Attribute 'items' must be an array, a Collection or a Map错误解决!

    唉!真的要说一句话叫做论一串代码的重要性!就是如此的气人!气的牙根痒痒! 前几天刚刚写过SpringMVC之ModelAndView的 jsp值在浏览页面不显示的问题!也是因为这一串代码,但是这一次一 ...

  9. python报错:TypeError: 'int' object is not subscriptable

    检查一遍报错的所在行,此报错一般是在整数上加了下标: 比如:   a = 4   c=a[2] 报错:line 2, in <module>    c=a[2] TypeError: 'i ...

  10. 用stringstream实现从数字到字符串的转化

    代码简单,字符串到数字和数字到字符串的写法类似. #include <sstream> #include <bits/stdc++.h> using namespace std ...