`
jayyanzhang2010
  • 浏览: 372901 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

关于HttpClient的总结

 
阅读更多

关于Httpclient的使用总结如下:

  1. (1)当HttpClient的实例不再需要时,可以使用连接管理器关闭   
  2. httpclient.getConnectionManager().shutdown();    
[java] view plaincopy
  1. (1)当HttpClient的实例不再需要时,可以使用连接管理器关闭  
  2. httpclient.getConnectionManager().shutdown();    
  1. (2)针对HTTPs的协议的HttpClient请求必须用户和密码   
  2.  httpclient.getCredentialsProvider()   
  3.             .setCredentials(new AuthScope("localhost"443),    
  4.                 new UsernamePasswordCredentials("username""password"));  
[java] view plaincopy
  1. (2)针对HTTPs的协议的HttpClient请求必须用户和密码  
  2.  httpclient.getCredentialsProvider()  
  3.             .setCredentials(new AuthScope("localhost"443),   
  4.                 new UsernamePasswordCredentials("username""password"));  
  1. (3)如果不想获取HTTPClient返回的信息   
  2.    httpclient.abort();  
[java] view plaincopy
  1. (3)如果不想获取HTTPClient返回的信息  
  2.    httpclient.abort();  
 
  1.       
  2. (4)httpclient传送文件的方式   
  3.         HttpClient httpclient = new DefaultHttpClient();   
  4.         HttpPost httppost = new HttpPost("http://www.apache.org");   
  5.         File file = new File(args[0]);   
  6.         InputStreamEntity reqEntity = new InputStreamEntity(   
  7.                 new FileInputStream(file), -1);   
  8.         reqEntity.setContentType("binary/octet-stream");   
  9.         reqEntity.setChunked(true);   
  10.         // It may be more appropriate to use FileEntity class in this particular   
  11.         // instance but we are using a more generic InputStreamEntity to demonstrate  
  12.         // the capability to stream out data from any arbitrary source  
  13.         //    
  14.         // FileEntity entity = new FileEntity(file, "binary/octet-stream");   
  15.         httppost.setEntity(reqEntity);   
  16.         System.out.println("executing request " + httppost.getRequestLine());   
  17.         HttpResponse response = httpclient.execute(httppost);  
[java] view plaincopy
  1.      
  2. (4)httpclient传送文件的方式  
  3.         HttpClient httpclient = new DefaultHttpClient();  
  4.         HttpPost httppost = new HttpPost("http://www.apache.org");  
  5.         File file = new File(args[0]);  
  6.         InputStreamEntity reqEntity = new InputStreamEntity(  
  7.                 new FileInputStream(file), -1);  
  8.         reqEntity.setContentType("binary/octet-stream");  
  9.         reqEntity.setChunked(true);  
  10.         // It may be more appropriate to use FileEntity class in this particular   
  11.         // instance but we are using a more generic InputStreamEntity to demonstrate  
  12.         // the capability to stream out data from any arbitrary source  
  13.         //   
  14.         // FileEntity entity = new FileEntity(file, "binary/octet-stream");   
  15.         httppost.setEntity(reqEntity);  
  16.         System.out.println("executing request " + httppost.getRequestLine());  
  17.         HttpResponse response = httpclient.execute(httppost);  
 
  1. (5)获取Cookie的信息   
  2.         HttpClient httpclient = new DefaultHttpClient();   
  3.         // 创建一个本地Cookie存储的实例   
  4.         CookieStore cookieStore = new BasicCookieStore();   
  5.         //创建一个本地上下文信息   
  6.         HttpContext localContext = new BasicHttpContext();   
  7.         //在本地上下问中绑定一个本地存储   
  8.         localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);   
  9.         //设置请求的路径   
  10.         HttpGet httpget = new HttpGet("http://www.google.com/");    
  11.         //传递本地的http上下文给服务器   
  12.         HttpResponse response = httpclient.execute(httpget, localContext);   
  13.         //获取本地信息   
  14.         HttpEntity entity = response.getEntity();   
  15.         System.out.println(response.getStatusLine());   
  16.         if (entity != null) {   
  17.             System.out.println("Response content length: " + entity.getContentLength());   
  18.         }   
  19.         //获取cookie中的各种信息   
  20.         List<Cookie> cookies = cookieStore.getCookies();   
  21.         for (int i = 0; i < cookies.size(); i++) {   
  22.             System.out.println("Local cookie: " + cookies.get(i));   
  23.         }   
  24.         //获取消息头的信息   
  25.         Header[] headers = response.getAllHeaders();   
  26.         for (int i = 0; i<headers.length; i++) {   
  27.             System.out.println(headers[i]);   
  28.         }  
[java] view plaincopy
  1. (5)获取Cookie的信息  
  2.         HttpClient httpclient = new DefaultHttpClient();  
  3.         // 创建一个本地Cookie存储的实例  
  4.         CookieStore cookieStore = new BasicCookieStore();  
  5.         //创建一个本地上下文信息  
  6.         HttpContext localContext = new BasicHttpContext();  
  7.         //在本地上下问中绑定一个本地存储  
  8.         localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);  
  9.         //设置请求的路径  
  10.         HttpGet httpget = new HttpGet("http://www.google.com/");   
  11.         //传递本地的http上下文给服务器  
  12.         HttpResponse response = httpclient.execute(httpget, localContext);  
  13.         //获取本地信息  
  14.         HttpEntity entity = response.getEntity();  
  15.         System.out.println(response.getStatusLine());  
  16.         if (entity != null) {  
  17.             System.out.println("Response content length: " + entity.getContentLength());  
  18.         }  
  19.         //获取cookie中的各种信息  
  20.         List<Cookie> cookies = cookieStore.getCookies();  
  21.         for (int i = 0; i < cookies.size(); i++) {  
  22.             System.out.println("Local cookie: " + cookies.get(i));  
  23.         }  
  24.         //获取消息头的信息  
  25.         Header[] headers = response.getAllHeaders();  
  26.         for (int i = 0; i<headers.length; i++) {  
  27.             System.out.println(headers[i]);  
  28.         }  
 
  1. (6)针对典型的SSL请求的处理   
  2.         DefaultHttpClient httpclient = new DefaultHttpClient();   
  3.         //获取默认的存储密钥类   
  4.         KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());    
  5.         //加载本地的密钥信息          
  6.         FileInputStream instream = new FileInputStream(new File("my.keystore"));    
  7.         try {   
  8.             trustStore.load(instream, "nopassword".toCharArray());   
  9.         } finally {   
  10.             instream.close();   
  11.         }   
  12.         //创建SSLSocketFactory,创建相关的Socket   
  13.         SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);   
  14.         //设置协议的类型和密钥信息,以及断开信息   
  15.         Scheme sch = new Scheme("https", socketFactory, 443);   
  16.         //在连接管理器中注册中信息   
  17.         httpclient.getConnectionManager().getSchemeRegistry().register(sch);  
[java] view plaincopy
  1. (6)针对典型的SSL请求的处理  
  2.         DefaultHttpClient httpclient = new DefaultHttpClient();  
  3.         //获取默认的存储密钥类  
  4.         KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());   
  5.         //加载本地的密钥信息         
  6.         FileInputStream instream = new FileInputStream(new File("my.keystore"));   
  7.         try {  
  8.             trustStore.load(instream, "nopassword".toCharArray());  
  9.         } finally {  
  10.             instream.close();  
  11.         }  
  12.         //创建SSLSocketFactory,创建相关的Socket  
  13.         SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);  
  14.         //设置协议的类型和密钥信息,以及断开信息  
  15.         Scheme sch = new Scheme("https", socketFactory, 443);  
  16.         //在连接管理器中注册中信息  
  17.         httpclient.getConnectionManager().getSchemeRegistry().register(sch);  
 
  1. (7)设置请求的参数的几种方式   
  2. A.在请求的路径中以查询字符串格式传递参数   
  3. B.在请求的实体中添加参数   
  4.         List <NameValuePair> nvps = new ArrayList <NameValuePair>();   
  5.         nvps.add(new BasicNameValuePair("IDToken1""username"));   
  6.         nvps.add(new BasicNameValuePair("IDToken2""password"));   
  7.         httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); 
分享到:
评论

相关推荐

    HttpClient总结.doc

    借助Java Secure Socket Extension (JSSE),HttpClient全面支持Secure Sockets Layer (SSL)或IETF Transport Layer Security (TLS)协议上的HTTP。

    HttpClient学习总结.docx

    HttpClient学习总结.docx

    [享学Feign] 九、Feign + OkHttp和Feign + Apache HttpClient哪个更香?

    目录前言正文OkHttp使用示例源码解析Apache HttpClient使用示例源码解析GoogleHttpClient哪个更香?总结声明 前言 前八篇文章介绍完了feign-core核心内容,从本篇开始将介绍它的“其它模块”。其实核心模块可以独立...

    C#HTTPclient 实例应用

    学习C#必经之路,入门级知识总结,笔记本知识概况,走一步再走一步

    HttpClient PostMethod 上传文件

    本人自己总结的httpClient PostMethod 上传文件完整实例 以及 使用java PostMethod 和GetMethod 发送请求实例 及乱码处理

    httpclient4

    httpclient,关于模拟登录的问题。从网站上下载下来的,归于一个总结

    使用 HttpClient 和 HtmlParser 实现简易爬虫

    使用HttpClient和HtmlParser实现网页爬虫,这个文档是我在学习使用后的总结,希望能帮助需要正在学习这方面的同学,能够更快的入门,以及一些更深入的了解。

    详解使用angular的HttpClient搭配rxjs

    一、原Http使用总结 使用方法 1.在根模块或核心模块引入HttpModule 即在AppModule或CoreModule中引入HttpModule: import { HttpModule } from '@angular/http'; @NgModule({ import: [ HttpModule ] // ... })...

    HTTP Client请求公共方法HttpUtils

    不同的服务,不同的系统之前的数据交互是在所难免的, HttpClient在不同的服务之前请求数据使用广泛,而且也会有各种各样的问题,SO 总结一些公共方法,避免重复造车

    android开发常用网络样例总结

    分别使用6个框架HttpURLConnection、HttpClient、AndroidAsyncHttp、Volley、OkHttp、Retrofit2 包中有服务器数据,可做样例数据使用 详细总结:http://blog.csdn.net/ahmclishihao/article/details/52861285#t0

    WebSerices异步调用方法总结

    研究Webservice异步调用的实现, 1、通过异步调用的方式实现高性能的使用WebService的API

    使用java发送get和post请求实践

    总结 在这个示例中,我们使用 Apache HttpClient 库来发送 GET 和 POST 请求,并处理响应。在 Java 中使用 Apache HttpClient 库可以非常方便地发送 HTTP 请求和处理响应。同时,我们也使用了 Logger 来记录日志,...

    java研究室,包含了各种小程序:文件上传,httpclient操作,数据库访问,图片操作等,方便在工作中快速取用.zip

    总结来说,【小程序名称】凭借其小巧便携、快捷高效的特性,不仅节省了用户的手机存储空间,更为用户提供了无缝衔接的便利服务,是现代生活中不可或缺的一部分,真正实现了“触手可及”的智能生活新体验。...

    BSP Async SSP ASP 的比较

    分布式机器学习四种同步机制的简单总结,包括BSP(Bulk Synchronous Parallel)批量同步并行、Async(Asynchronous Parallel)全异步并行、SSP(Stale Synchronous Parallel)延迟同步并行、ASP大概同步并行。

    PB_WinCE.rar_Platform Builder 5_platform builder_wince fat_windo

    5 总结 9 Platform Builder之旅(一) 10 Platform Builder之旅(二) 12 Platform Builder之旅(三) 15 一、源码配置文件 15 二、镜像配置文件: 16 Platform Builder之旅(四) 22 【Windows CE安装目录】 22 ...

    最常用的java技术总结

    本xmind包含:Springmvc,Mybatis,HttpClient,shiro,redis,Mysql,Elasticsearch,ActiveMQ,nginx,SpringTest,jsonp,阿里大鱼,echarts/higcharte

    java 网络及通信

    java网络及通信、httpClient、socket通信、HttpURLConnection、java 中常用的几种网络通信方式。在此总结、希望对需要的同仁有所帮助。

    手把手从零搭建新冠疫情防控指挥作战平台视频教程

    3,综合运用HttpClient+Jsoup+Kafka+SparkStreaming+StructuredStreaming+SpringBoot+Echarts等多种实用技术 适用人群 1、对大数据感兴趣的在校生及应届毕业生。 2、对目前职业有进一步提升要求,希望从事大数据...

    Android开发技巧总汇(个人总结)

    17.通过HttpClient从指定server获取数据 22 18.拖动Button获得位置 23 19.代码安装apk包 25 20.给模拟器打电话发短信 26 21.从google搜索内容 26 22.浏览网页 26 23.显示地图 26 25.拨打电话 27 26.调用发...

    文件上传源码 客户端服务端

    socket httpclient 这个功能经常需要用到,索性完整的总结一下,包括客户端服务端,上传至sae

Global site tag (gtag.js) - Google Analytics