概述
关于Httpclient的使用总结如下:
- (1)当HttpClient的实例不再需要时,可以使用连接管理器关闭
- httpclient.getConnectionManager().shutdown();
- (1)当HttpClient的实例不再需要时,可以使用连接管理器关闭
- httpclient.getConnectionManager().shutdown();
(1)当HttpClient的实例不再需要时,可以使用连接管理器关闭
httpclient.getConnectionManager().shutdown();
- (2)针对HTTPs的协议的HttpClient请求必须用户和密码
- httpclient.getCredentialsProvider()
- .setCredentials(new AuthScope("localhost", 443),
- new UsernamePasswordCredentials("username", "password"));
- (2)针对HTTPs的协议的HttpClient请求必须用户和密码
- httpclient.getCredentialsProvider()
- .setCredentials(new AuthScope("localhost", 443),
- new UsernamePasswordCredentials("username", "password"));
(2)针对HTTPs的协议的HttpClient请求必须用户和密码
httpclient.getCredentialsProvider()
.setCredentials(new AuthScope("localhost", 443),
new UsernamePasswordCredentials("username", "password"));
- (3)如果不想获取HTTPClient返回的信息
- httpclient.abort();
- (3)如果不想获取HTTPClient返回的信息
- httpclient.abort();
(3)如果不想获取HTTPClient返回的信息
httpclient.abort();
- (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);
- (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);
(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);
- (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]);
- }
- (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]);
- }
(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]);
}
- (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
- SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
- //设置协议的类型和密钥信息,以及断开信息
- Scheme sch = new Scheme("https", socketFactory, 443);
- //在连接管理器中注册中信息
- httpclient.getConnectionManager().getSchemeRegistry().register(sch);
- (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
- SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
- //设置协议的类型和密钥信息,以及断开信息
- Scheme sch = new Scheme("https", socketFactory, 443);
- //在连接管理器中注册中信息
- httpclient.getConnectionManager().getSchemeRegistry().register(sch);
(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
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
//设置协议的类型和密钥信息,以及断开信息
Scheme sch = new Scheme("https", socketFactory, 443);
//在连接管理器中注册中信息
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
- (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的总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复