关于HttpClient的总结
2017-08-16 14:48
344 查看
关于Httpclient的使用总结如下:
[java]
view plain
copy
(1)当HttpClient的实例不再需要时,可以使用连接管理器关闭
httpclient.getConnectionManager().shutdown();
[java]
view plain
copy
(2)针对HTTPs的协议的HttpClient请求必须用户和密码
httpclient.getCredentialsProvider()
.setCredentials(new AuthScope("localhost", 443),
new UsernamePasswordCredentials("username", "password"));
[java]
view plain
copy
(3)如果不想获取HTTPClient返回的信息
httpclient.abort();
[java]
view plain
copy
(4)httpclient传送文件的方式
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.apache.org");
File file = new File(args[0]);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true);
// It may be more appropriate to use FileEntity class in this particular
// instance but we are using a more generic InputStreamEntity to demonstrate
// the capability to stream out data from any arbitrary source
//
// FileEntity entity = new FileEntity(file, "binary/octet-stream");
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
[java]
view plain
copy
(5)获取Cookie的信息
HttpClient httpclient = new DefaultHttpClient();
// 创建一个本地Cookie存储的实例
CookieStore cookieStore = new BasicCookieStore();
//创建一个本地上下文信息
HttpContext localContext = new BasicHttpContext();
//在本地上下问中绑定一个本地存储
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
//设置请求的路径
HttpGet httpget = new HttpGet("http://www.google.com/");
//传递本地的http上下文给服务器
HttpResponse response = httpclient.execute(httpget, localContext);
//获取本地信息
HttpEntity entity = response.getEntity();
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
//获取cookie中的各种信息
List<Cookie> cookies = cookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + cookies.get(i));
}
//获取消息头的信息
Header[] headers = response.getAllHeaders();
for (int i = 0; i<headers.length; i++) {
System.out.println(headers[i]);
}
[java]
view plain
copy
(6)针对典型的SSL请求的处理
DefaultHttpClient httpclient = new DefaultHttpClient();
//获取默认的存储密钥类
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
//加载本地的密钥信息
FileInputStream instream = new FileInputStream(new File("my.keystore"));
try {
trustStore.load(instream, "nopassword".toCharArray());
} finally {
instream.close();
}
//创建SSLSocketFactory,创建相关的Socket
SSLSocke
b95e
tFactory socketFactory = new SSLSocketFactory(trustStore);
//设置协议的类型和密钥信息,以及断开信息
Scheme sch = new Scheme("https", socketFactory, 443);
//在连接管理器中注册中信息
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
[java]
view plain
copy
(7)设置请求的参数的几种方式
A.在请求的路径中以查询字符串格式传递参数
B.在请求的实体中添加参数
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("IDToken1", "username"));
nvps.add(new BasicNameValuePair("IDToken2", "password"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
[java]
view plain
copy
(1)当HttpClient的实例不再需要时,可以使用连接管理器关闭
httpclient.getConnectionManager().shutdown();
[java]
view plain
copy
(2)针对HTTPs的协议的HttpClient请求必须用户和密码
httpclient.getCredentialsProvider()
.setCredentials(new AuthScope("localhost", 443),
new UsernamePasswordCredentials("username", "password"));
[java]
view plain
copy
(3)如果不想获取HTTPClient返回的信息
httpclient.abort();
[java]
view plain
copy
(4)httpclient传送文件的方式
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.apache.org");
File file = new File(args[0]);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true);
// It may be more appropriate to use FileEntity class in this particular
// instance but we are using a more generic InputStreamEntity to demonstrate
// the capability to stream out data from any arbitrary source
//
// FileEntity entity = new FileEntity(file, "binary/octet-stream");
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
[java]
view plain
copy
(5)获取Cookie的信息
HttpClient httpclient = new DefaultHttpClient();
// 创建一个本地Cookie存储的实例
CookieStore cookieStore = new BasicCookieStore();
//创建一个本地上下文信息
HttpContext localContext = new BasicHttpContext();
//在本地上下问中绑定一个本地存储
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
//设置请求的路径
HttpGet httpget = new HttpGet("http://www.google.com/");
//传递本地的http上下文给服务器
HttpResponse response = httpclient.execute(httpget, localContext);
//获取本地信息
HttpEntity entity = response.getEntity();
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
//获取cookie中的各种信息
List<Cookie> cookies = cookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + cookies.get(i));
}
//获取消息头的信息
Header[] headers = response.getAllHeaders();
for (int i = 0; i<headers.length; i++) {
System.out.println(headers[i]);
}
[java]
view plain
copy
(6)针对典型的SSL请求的处理
DefaultHttpClient httpclient = new DefaultHttpClient();
//获取默认的存储密钥类
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
//加载本地的密钥信息
FileInputStream instream = new FileInputStream(new File("my.keystore"));
try {
trustStore.load(instream, "nopassword".toCharArray());
} finally {
instream.close();
}
//创建SSLSocketFactory,创建相关的Socket
SSLSocke
b95e
tFactory socketFactory = new SSLSocketFactory(trustStore);
//设置协议的类型和密钥信息,以及断开信息
Scheme sch = new Scheme("https", socketFactory, 443);
//在连接管理器中注册中信息
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
[java]
view plain
copy
(7)设置请求的参数的几种方式
A.在请求的路径中以查询字符串格式传递参数
B.在请求的实体中添加参数
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("IDToken1", "username"));
nvps.add(new BasicNameValuePair("IDToken2", "password"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
相关文章推荐
- 关于HttpClient的总结
- 关于HttpClient的总结
- 关于Android网络访问请求的一些总结 包括HttpClient HttpUrlConnection Volley
- 关于HttpClient的总结
- 关于HttpClient在服务器端使用请求碰到的问题总结
- 关于HttpClient的总结
- 关于HttpClient的总结(一)
- 关于HttpClient的总结
- 关于HttpClient的总结
- 关于HttpClient的总结
- 关于Xutils框架出现无法访问HttpRequestBase 找不到org.apache.http.client.methods.HttpRequestBase解决方案
- 关于Http中的状态码总结
- 关于org.apache.http包中的HttpClient在myeclipse中无法解析问题
- 关于android中HttpClient的cookie
- 关于微软HttpClient使用,避免踩坑
- 关于安卓HTTP请求用HttpUrlConnection还是HttpClient好
- HttpClient和HtmlUnit的比较总结以及使用技巧(一)
- java HttpClient学习总结:基本http操作
- OKhttpClient 简单使用总结
- SpringCloud关于@FeignClient和Hystrix集成对http线程池监控问题