关于HttpClient的总结
2013-09-12 18:56
323 查看
原文地址:http://blog.csdn.net/shimiso/article/details/7047447
关于Httpclient的使用总结如下:
-----------------------------------------------------
注意,这篇博客总结的不错,学习一下,多谢博主分享哦
关于Httpclient的使用总结如下:
(1)当HttpClient的实例不再需要时,可以使用连接管理器关闭 httpclient.getConnectionManager().shutdown();
(2)针对HTTPs的协议的HttpClient请求必须用户和密码 httpclient.getCredentialsProvider() .setCredentials(new AuthScope("localhost", 443), new UsernamePasswordCredentials("username", "password"));
(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);
(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);
(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的总结(一)
- 关于HttpClient的总结
- 关于HttpClient的总结
- 关于HttpClient的总结
- 关于HttpClient的总结
- 关于HttpClient的总结
- 关于HttpClient在服务器端使用请求碰到的问题总结
- 关于Android网络访问请求的一些总结 包括HttpClient HttpUrlConnection Volley
- 关于HttpClient的总结
- HttpClient解析页面总结
- 【Android-HTTP】关于HTTP方面的总结
- HttpClient和HtmlUnit的比较总结
- 关于Https协议和HttpClient的实现详解
- (十五)httpClient、HttpUrlConnecttion调研总结
- 第一行代码总结:10网络:10.2.2使用HttpClient
- 关于HttpClient 获取页面,出现乱码的问题
- HttpClient使用详解总结大全①
- ranong项目总结-HttpClient-Timeout设置(一)
- 关于HttpClientGet的具体用法