上篇主要介绍了HttpClient的一些简介及其基本的请求执行过程,下面继续对请求工程中涉及到的类及方法进行学习。
Demo
请求
HttpClient支持所有定义在HTTP/1.1版本中的HTTP方法:GET,HEAD,POST,PUT,DELETE,TRACE和OPTIONS。对于每个方法类型都有一个特殊的类:HttpGet,HttpHead,HttpPost,HttpPut,HttpDelete,HttpTrace和HttpOptions去完成。
请求的URI是统一资源定位符,它标识了应用于哪个请求之上的资源,也即我们想要请求的接口地址。HTTP请求的URI包含一个协议模式,主机名称,可选的端口,资源路径,可选的查询和可选的片段。比如上例中这个天气预报的URI:
HttpClient提供很多工具方法来简化创建和修改执行URI。
URI也可以编程来拼装:
输出内容为:
查询字符串也可以从独立的参数中来生成:
输出内容为:
说明:URLEncodedUtils用于转换中文为UTF-8,防止中文乱码。
说明:定义了一个NameValuePair类型的list集合,NameValuePair顾名思义名值对,多处用于Java像url发送Post请求。即这里我们在发送post请求时用该list来存放参数。
Demo
先来看下基本的请求响应的一个过程(Get类型为例,获取天气预报的一个http接口),以此我们来进行研究:
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
public class Test {
public static void main(String[] args) {
// 核心应用类
HttpClient httpClient = new DefaultHttpClient();
// HTTP请求
HttpGet request = new HttpGet("http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=101010100&weatherType=0");
// 打印请求信息
System.out.println(request.getRequestLine());
try {
// 发送请求,返回响应
HttpResponse response = httpClient.execute(request);
// 打印响应信息
System.out.println(response.getStatusLine());
} catch (ClientProtocolException e) {
// 协议错误
e.printStackTrace();
} catch (IOException e) {
// 网络异常
e.printStackTrace();
}
}
}
请求
HttpClient支持所有定义在HTTP/1.1版本中的HTTP方法:GET,HEAD,POST,PUT,DELETE,TRACE和OPTIONS。对于每个方法类型都有一个特殊的类:HttpGet,HttpHead,HttpPost,HttpPut,HttpDelete,HttpTrace和HttpOptions去完成。请求的URI是统一资源定位符,它标识了应用于哪个请求之上的资源,也即我们想要请求的接口地址。HTTP请求的URI包含一个协议模式,主机名称,可选的端口,资源路径,可选的查询和可选的片段。比如上例中这个天气预报的URI:
String uri = "http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=101010100&weatherType=0"
HttpGet httpget = new HttpGet(uri);
HttpClient提供很多工具方法来简化创建和修改执行URI。
URI也可以编程来拼装:
URI uri = URIUtils.createURI("http", "weather.51wnl.com", -1, "/weatherinfo/GetMoreWeather","cityCode=101010100&weatherType=0", null);
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());
输出内容为:
http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=101010100&weatherType=0
查询字符串也可以从独立的参数中来生成:
List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("cityCode", "101010100"));
qparams.add(new BasicNameValuePair("weatherType", "0"));
URI uri = URIUtils.createURI("http", "weather.51wnl.com", -1, "/weatherinfo/GetMoreWeather",
URLEncodedUtils.format(qparams, "UTF-8"), null);
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());
输出内容为:
http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=101010100&weatherType=0
说明:URLEncodedUtils用于转换中文为UTF-8,防止中文乱码。
对于Post提交,参数通过HttpEntity对象进行承载:
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
formParams.add(new BasicNameValuePair("cityCode", "101010100"));
formParams.add(new BasicNameValuePair("weatherType", "0"));
HttpEntity entity = new UrlEncodedFormEntity(formParams, "UTF-8");
HttpPost request = new HttpPost(“http://weather.51wnl.com/weatherinfo/GetMoreWeather”);
request.setEntity(entity);
说明:定义了一个NameValuePair类型的list集合,NameValuePair顾名思义名值对,多处用于Java像url发送Post请求。即这里我们在发送post请求时用该list来存放参数。
响应
HttpClient程序包对于HTTP响应的处理较之HTTP请求来说是简单多了,其过程同样使用了HttpEntity接口。我们可以从HttpEntity对象中取出数据流(InputStream),该数据流就是服务器返回的响应数据。需要注意的是,HttpClient程序包不负责解析数据流中的内容。如: HttpUriRequest request = ...;
HttpResponse response = httpClient.execute(request);
// 从response中取出HttpEntity对象
HttpEntity entity = response.getEntity();
// 查看entity的各种指标
System.out.println(entity.getContentType());
System.out.println(entity.getContentLength());
System.out.println(EntityUtils.getContentCharSet(entity));
// 取出服务器返回的数据流
InputStream stream = entity.getContent();
// 或者
String resultBody = EntityUtils.toString(entity, “utf-8”);
小结
对HttpClient的请求和响应过程有了清晰的认识,这也是在http中最常用的功能,从上面的代码中我们还可以看到url还是可以在进行抽象封装的,后面继续对get和post方法进行封装。