关于Httpclient的使用总结如下:
- (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"));
- (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);
- (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);
- (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 Secure Socket Extension (JSSE),HttpClient全面支持Secure Sockets Layer (SSL)或IETF Transport Layer Security (TLS)协议上的HTTP。
HttpClient学习总结.docx
目录前言正文OkHttp使用示例源码解析Apache HttpClient使用示例源码解析GoogleHttpClient哪个更香?总结声明 前言 前八篇文章介绍完了feign-core核心内容,从本篇开始将介绍它的“其它模块”。其实核心模块可以独立...
学习C#必经之路,入门级知识总结,笔记本知识概况,走一步再走一步
本人自己总结的httpClient PostMethod 上传文件完整实例 以及 使用java PostMethod 和GetMethod 发送请求实例 及乱码处理
httpclient,关于模拟登录的问题。从网站上下载下来的,归于一个总结
使用HttpClient和HtmlParser实现网页爬虫,这个文档是我在学习使用后的总结,希望能帮助需要正在学习这方面的同学,能够更快的入门,以及一些更深入的了解。
一、原Http使用总结 使用方法 1.在根模块或核心模块引入HttpModule 即在AppModule或CoreModule中引入HttpModule: import { HttpModule } from '@angular/http'; @NgModule({ import: [ HttpModule ] // ... })...
不同的服务,不同的系统之前的数据交互是在所难免的, HttpClient在不同的服务之前请求数据使用广泛,而且也会有各种各样的问题,SO 总结一些公共方法,避免重复造车
分别使用6个框架HttpURLConnection、HttpClient、AndroidAsyncHttp、Volley、OkHttp、Retrofit2 包中有服务器数据,可做样例数据使用 详细总结:http://blog.csdn.net/ahmclishihao/article/details/52861285#t0
研究Webservice异步调用的实现, 1、通过异步调用的方式实现高性能的使用WebService的API
总结来说,【小程序名称】凭借其小巧便携、快捷高效的特性,不仅节省了用户的手机存储空间,更为用户提供了无缝衔接的便利服务,是现代生活中不可或缺的一部分,真正实现了“触手可及”的智能生活新体验。...
分布式机器学习四种同步机制的简单总结,包括BSP(Bulk Synchronous Parallel)批量同步并行、Async(Asynchronous Parallel)全异步并行、SSP(Stale Synchronous Parallel)延迟同步并行、ASP大概同步并行。
5 总结 9 Platform Builder之旅(一) 10 Platform Builder之旅(二) 12 Platform Builder之旅(三) 15 一、源码配置文件 15 二、镜像配置文件: 16 Platform Builder之旅(四) 22 【Windows CE安装目录】 22 ...
本xmind包含:Springmvc,Mybatis,HttpClient,shiro,redis,Mysql,Elasticsearch,ActiveMQ,nginx,SpringTest,jsonp,阿里大鱼,echarts/higcharte
java网络及通信、httpClient、socket通信、HttpURLConnection、java 中常用的几种网络通信方式。在此总结、希望对需要的同仁有所帮助。
3,综合运用HttpClient+Jsoup+Kafka+SparkStreaming+StructuredStreaming+SpringBoot+Echarts等多种实用技术 适用人群 1、对大数据感兴趣的在校生及应届毕业生。 2、对目前职业有进一步提升要求,希望从事大数据...
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
因此,自己对此做了些总结,给出了我的一个解决方案。 首先,需要明确一下http通信流程,Android目前提供两种http通信方式,HttpURLConnection和HttpClient,HttpURLConnection多用于发送或接收流式数据,因此比较...