关于HttpClient的总结

发布时间:2014-10-22 14:53:41编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"关于HttpClient的总结",主要涉及到关于HttpClient的总结方面的内容,对于关于HttpClient的总结感兴趣的同学可以参考一下。

原文地址:http://blog.csdn.net/shimiso/article/details/7047447 关于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)); ----------------------------------------------------- 注意,这篇博客总结的不错,学习一下,多谢博主分享哦


上一篇:Linux系统在X86上的虚拟内存管理详解 (4)
下一篇:c++面试题之易错题本

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款