- 浏览: 1432112 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (363)
- oracle (43)
- 缓存策略 (4)
- 数据库设计 (7)
- IntelliJ IDEA (8)
- freemarker (3)
- JSP (12)
- java基础 (75)
- tomcat (14)
- 服务器 (10)
- maven (3)
- linux (21)
- e-learning (2)
- 手机开发 (1)
- 设计模式 (6)
- 技术之外 (2)
- 工具的使用 (8)
- HTML 5 (5)
- 网站前台 (38)
- 网站后台 (11)
- 项目管理 (12)
- 面试题 (5)
- 幽默 (0)
- 系统架构 (8)
- 创业 (10)
- div-css (5)
- 诗词歌赋 (1)
- 网络课堂组知识库 (4)
- 杂家 (3)
- android (12)
- 框架 (14)
- 读书笔记 (1)
- 孙子兵法 (1)
- mysql (3)
- 小人书 (0)
- Jquery (6)
- eclipse (2)
- 正则表达式 (1)
最新评论
-
a98709474:
写的很详细,看完了,明白了这种概念了
数据库水平切分(拆库拆表)的实现原理解析(转) -
a98709474:
MYSQL分库分表总结 -
CatchU:
7年前的招聘要求,厉害厉害
面试要求 -
luozhixiong:
<table class="bbcode&qu ...
MYSQL分库分表总结 -
沈寅麟:
好用
freemarker格式化
1:概述
HttpClient是HttpComponents(简称为hc)项目其中的一部份,访问地址:http://hc.apache.org/
HttpClient是一个代码级的Http客户端工具,可以使用它模拟浏览器向Http服务器发送请求。使用HttpClient还需要HttpCore.后者包括Http请求与Http响应的代码封装。
2:HttpGet
httpcore:EntityUtils.toString(httpEntity)
读取response响应内容部分,也可以借助于 httpcore-4.1.2.jar 里面的
具体:
执行结果:
httpget.setHeader
1:分析请求包中这六个头信息
2:代码
3:效果
以上乱码是由于
注释掉这行,具体:
效果:
用post方法访问本地应用根据传递参数不同,返回不同结果
web.xml
Test1Servlet.java
Demo2.java
结果:
HttpClient是HttpComponents(简称为hc)项目其中的一部份,访问地址:http://hc.apache.org/
HttpClient是一个代码级的Http客户端工具,可以使用它模拟浏览器向Http服务器发送请求。使用HttpClient还需要HttpCore.后者包括Http请求与Http响应的代码封装。
2:HttpGet
public final static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); try { HttpGet httpget = new HttpGet("http://www.apache.org/"); System.out.println("executing request " + httpget.getURI()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } System.out.println("----------------------------------------"); InputStream inSm = entity.getContent(); Scanner inScn = new Scanner(inSm); while (inScn.hasNextLine()) { System.out.println(inScn.nextLine()); } // Do not feel like reading the response body // Call abort on the request object httpget.abort(); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
httpcore:EntityUtils.toString(httpEntity)
读取response响应内容部分,也可以借助于 httpcore-4.1.2.jar 里面的
String content = EntityUtils.toString(httpEntity);
具体:
/** * User: liuwentao * Time: 12-1-25 下午1:23 */ public class Demo1 { /** * 用 get 方法访问 www.apache.org 并返回内容 * * @param args */ public static void main(String[] args) { //创建默认的 HttpClient 实例 HttpClient httpClient = new DefaultHttpClient(); try { //创建 httpUriRequest 实例 HttpGet httpGet = new HttpGet("http://www.apache.org/"); System.out.println("uri=" + httpGet.getURI()); //执行 get 请求 HttpResponse httpResponse = httpClient.execute(httpGet); //获取响应实体 HttpEntity httpEntity = httpResponse.getEntity(); //打印响应状态 System.out.println(httpResponse.getStatusLine()); if (httpEntity != null) { //响应内容的长度 long length = httpEntity.getContentLength(); //响应内容 String content = EntityUtils.toString(httpEntity); System.out.println("Response content length:" + length); System.out.println("Response content:" + content); } //有些教程里没有下面这行 httpGet.abort(); } catch (Exception e) { e.printStackTrace(); } finally { //关闭连接,释放资源 httpClient.getConnectionManager().shutdown(); } } }
执行结果:
httpget.setHeader
1:分析请求包中这六个头信息
2:代码
/** * @param args * @throws IOException */ public static void main(String[] args) { HttpClient httpClient = new DefaultHttpClient(); try { HttpGet httpget = new HttpGet("http://www.iteye.com"); httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); httpget.setHeader("Accept-Language", "zh-cn,zh;q=0.5"); httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1)"); httpget.setHeader("Accept-Encoding", "gzip, deflate"); httpget.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"); httpget.setHeader("Host", "www.iteye.com"); httpget.setHeader("Connection", "Keep-Alive"); HttpResponse response = httpClient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } System.out.println("----------------------------------------"); InputStream inSm = entity.getContent(); Scanner inScn = new Scanner(inSm); while (inScn.hasNextLine()) { System.out.println(inScn.nextLine()); } // Do not feel like reading the response body // Call abort on the request object httpget.abort(); } catch (Exception e) { e.printStackTrace(); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpClient.getConnectionManager().shutdown(); } }
3:效果
以上乱码是由于
httpget.setHeader("Accept-Encoding", "gzip, deflate");
注释掉这行,具体:
/** * User: liuwentao * Time: 12-1-25 下午1:23 */ public class Demo3 { /** * 用 get 方法访问 www.baidu.com 并返回内容 * * @param args */ public static void main(String[] args) { //创建默认的 HttpClient 实例 HttpClient httpClient = new DefaultHttpClient(); try { //创建 httpUriRequest 实例 HttpGet httpGet = new HttpGet("http://www.iteye.com"); httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); httpGet.setHeader("Accept-Language", "zh-cn,zh;q=0.5"); httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1)"); // httpGet.setHeader("Accept-Encoding", "gzip, deflate"); httpGet.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"); httpGet.setHeader("Host", "www.iteye.com"); httpGet.setHeader("Connection", "Keep-Alive"); System.out.println("uri=" + httpGet.getURI()); //执行 get 请求 HttpResponse httpResponse = httpClient.execute(httpGet); //获取响应实体 HttpEntity httpEntity = httpResponse.getEntity(); //打印响应状态 System.out.println(httpResponse.getStatusLine()); if (httpEntity != null) { //响应内容的长度 long length = httpEntity.getContentLength(); //响应内容 String content = EntityUtils.toString(httpEntity); System.out.println("Response content length:" + length); System.out.println("Response content:" + content); } //有些教程里没有下面这行 httpGet.abort(); } catch (Exception e) { e.printStackTrace(); } finally { //关闭连接,释放资源 httpClient.getConnectionManager().shutdown(); } } }
效果:
用post方法访问本地应用根据传递参数不同,返回不同结果
web.xml
<servlet> <servlet-name>Test1Servlet</servlet-name> <servlet-class>demo.servlet.Test1Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Test1Servlet</servlet-name> <url-pattern>/test1Servlet</url-pattern> </servlet-mapping>
Test1Servlet.java
/** * User: liuwentao * Time: 11-12-25 下午1:08 */ public class Test1Servlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接收地址栏参数 String param1 = request.getParameter("param1"); //输出 response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.write("你传递来的参数param1=" + param1); out.close(); } }
Demo2.java
/** * User: liuwentao * Time: 12-1-25 下午1:23 */ public class Demo2 { /** * 用post方法访问本地应用根据传递参数不同,返回不同结果 * * @param args */ public static void main(String[] args) { //创建默认的 HttpClient 实例 HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://localhost:86/test1Servlet"); List<NameValuePair> formParams = new ArrayList<NameValuePair>(); formParams.add(new BasicNameValuePair("param1", "刘文涛")); UrlEncodedFormEntity urlEncodedFormEntity; try { urlEncodedFormEntity = new UrlEncodedFormEntity(formParams, "UTF-8"); httpPost.setEntity(urlEncodedFormEntity); System.out.println("execurting request:" + httpPost.getURI()); HttpResponse httpResponse = null; httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { String content = EntityUtils.toString(httpEntity, "UTF-8"); System.out.println("Response content:" + content); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { //关闭连接,释放资源 httpClient.getConnectionManager().shutdown(); } } }
结果:
评论
5 楼
dyingearth
2014-12-01
讲得太好了,赞一个
4 楼
java_ganbin
2014-03-25
文涛兄 来学习了 嘻嘻
3 楼
黑豹1231
2013-11-09
真的很不错,学习了
2 楼
abc382410124
2013-08-23
柳暗花明又一村的感觉
1 楼
xiaogaogao
2013-04-15
,精彩的讲解,收益非浅,学习中
发表评论
-
打印IP 来源
2014-10-16 22:07 946<% String userAgent = reques ... -
java 写文本换行
2014-08-08 18:34 1977import org.apache.commons.io.Fi ... -
DBCP连接池配置示例
2014-06-17 11:44 1247<bean id="dataSourceOra ... -
CGLIB与asm
2014-05-05 14:42 1349“程序运行时,允许改变程序结构或变量类型,这种语言称为动态语言 ... -
201404转成 2014.04
2014-03-31 17:45 999public static void main(Str ... -
取出 字符串中的 网址
2013-10-15 17:47 1269public static void main(Str ... -
java反国际化
2013-09-23 18:10 1017public static String fromEn ... -
AtomicInteger与Volatile修饰
2013-02-05 11:50 6049今天写代码,尝试使用了AtomicInteger这个类,感觉 ... -
ExecutorService线程池
2013-02-05 11:40 1625http://blog.sina.com.cn/s/blog ... -
web.xml中load-on-startup的作用
2012-12-24 10:06 1666如下一段配置,熟悉DWR的再熟悉不过了: <servle ... -
最新版本
2012-09-13 09:28 1013Java SE 7u7 apache-tomcat-7.0. ... -
格式化数字
2012-09-10 20:12 907public static void main(Str ... -
用apache common io包获得文件扩展名
2012-09-09 23:11 1372apache common io包包含了很多非常实用的工具类, ... -
值传递,引用传递
2012-07-28 23:29 1147java 对于 8种基本类型 和 他们的包装类型 , 外 ... -
代码之丑
2012-07-07 22:01 2400原文:http://www.infoq.com/cn/news ... -
Memcache
2012-06-27 09:36 1047Memcache是一个高性能的分布式的内存对象缓存系统,通过在 ... -
ActiveMQ
2012-06-14 15:09 15160ActiveMQ 是Apache出品,最流行的,能力强劲的开源 ... -
proxool配置
2012-06-12 11:43 1186项目结构 所需jar包 proxool.prope ... -
各种文件的注释
2012-04-23 10:44 1012<%-- comment --%> JSP注释,也称为“隐藏注 ... -
List排序
2012-03-27 20:18 1204集合类List存放的数据,默认是按照放入时的顺序存放的,比如依 ...
相关推荐
《深入解析httpclient.jar及其与code.jar的关联》 在Java开发中,HTTP通信是不可或缺的一部分,而Apache HttpClient库正是Java实现HTTP客户端操作的重要工具。本文将深入探讨httpclient.jar包,以及它与code.jar包...
### Httpclient官网教程中文版知识点总结 #### 一、引言 HTTP协议作为互联网的核心通信标准之一,在现代网络服务及物联网设备中扮演着至关重要的角色。随着技术的发展,越来越多的应用和服务依赖于HTTP协议来实现...
HttpClient 4.2.1版本引入了一些重要的改进和修复,以提高性能和稳定性。以下是一些关键特性: 1. **连接管理**:HttpClient 4.2.1引入了更完善的连接管理机制,允许开发者控制连接的创建、复用和关闭。`...
例如,在HttpClient 3.x中,代码可能会使用`***mons.httpclient.HttpClient`类和`***mons.httpclient.methods.GetMethod`等,而在4.x版本中,这些都被新的API所替代。程序员需要熟悉`org.apache....
赠送jar包:httpclient-4.2.5.jar; 赠送原API文档:httpclient-4.2.5-javadoc.jar; 赠送源代码:httpclient-4.2.5-sources.jar; 赠送Maven依赖信息文件:httpclient-4.2.5.pom; 包含翻译后的API文档:httpclient...
本文将深入探讨HttpClient 4.2.1的核心特性和使用方法,帮助开发者更好地理解和应用这个强大的工具。 一、HttpClient简介 HttpClient是一个开放源码的Java库,由Apache软件基金会维护。它为Java程序员提供了一个...