早就如雷贯耳它的大名,却一直不曾相见,昨天下载下来,今天终于测试了一把,用的官网的QUICK START例子,来访问我自己以前开发过的WEB程序,因为这个网站恰好有一个写好的通过POST请求验证用户名密码进行登录的功能。

下面是QUICK START的代码:

public class QuickStart {

    public static void main(String[] args) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpGet httpGet = new HttpGet("http://localhost:8080/login.html");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
// The underlying HTTP connection is still held by the response
// object
// to allow the response content to be streamed directly from the
// network socket.
// In order to ensure correct deallocation of system resources
// the user MUST call CloseableHttpResponse#close() from a finally
// clause.
// Please note that if response content is not fully consumed the
// underlying
// connection cannot be safely re-used and will be shut down and
// discarded
// by the connection manager.
try {
System.out.println(response1.getStatusLine());
HttpEntity entity1 = response1.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity1);
} finally {
response1.close();
} HttpPost httpPost = new HttpPost(
"http://localhost:8080/loginvalidate.html");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", "shalltear"));
nvps.add(new BasicNameValuePair("password", "123123"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost); try {
System.out.println(response2.getStatusLine());
System.out.println(response2.getAllHeaders());
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity2);
} finally {
response2.close();
}
} finally {
httpclient.close();
}
} }

运行之后,WEB端收到了请求,以下是控制台的调试信息,可以看到已经验证通过
16:56:22.451 [http-8080-2] DEBUG org.mybatis.spring.SqlSessionUtils – Creating a new SqlSession
16:56:22.452 [http-8080-2] DEBUG org.mybatis.spring.SqlSessionUtils – SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@29bf9bb9] was not registered for synchronization because synchronization is not active
16:56:22.452 [http-8080-2] DEBUG o.m.s.t.SpringManagedTransaction – JDBC Connection [jdbc:oracle:thin:@localhost:1521/blogdb, UserName=SHALLTEAR, Oracle JDBC driver] will not be managed by Spring
16:56:22.452 [http-8080-2] DEBUG c.b.i.C.getPasswordByUserName – ==> Preparing: select password from t_password a where a.userid=(select b.userid from t_user b where b.username=?)
16:56:22.452 [http-8080-2] DEBUG c.b.i.C.getPasswordByUserName – ==> Parameters: shalltear(String)
16:56:22.455 [http-8080-2] DEBUG c.b.i.C.getPasswordByUserName – <== Total: 1 16:56:22.455 [http-8080-2] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@29bf9bb9] 16:56:22.455 [http-8080-2] DEBUG com.blog.dao.DaoImpl - passworddb: 8C52732AAAA8B64ABDB02F913522AAAA 16:56:22.455 [http-8080-2] DEBUG com.blog.dao.DaoImpl - passwordmd5: 8C52732AAAA8B64ABDB02F913522AAAA 16:56:22.455 [http-8080-2] DEBUG com.blog.dao.DaoImpl - 登陆成功

HTTPCLIENT 学习 (1) 入门的更多相关文章

  1. HttpClient学习整理

    HttpClient简介HttpClient 功能介绍    1. 读取网页(HTTP/HTTPS)内容    2.使用POST方式提交数据(httpClient3)    3. 处理页面重定向    ...

  2. HttpClient 学习整理【转】

    转自 http://www.blogjava.net/Alpha/archive/2007/01/22/95216.html HttpClient 是我最近想研究的东西,以前想过的一些应用没能有很好的 ...

  3. HttpClient 学习整理 (转)

    source:http://www.blogjava.net/Alpha/archive/2007/01/22/95216.html HttpClient 是我最近想研究的东西,以前想过的一些应用没能 ...

  4. Python学习--01入门

    Python学习--01入门 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.和PHP一样,它是后端开发语言. 如果有C语言.PHP语言.JAVA语言等其中一种语言的基础,学习Py ...

  5. [IT学习]sql 入门及实例

    sql 是一种数据库查询语言,可以让你很快的查询到数据.其实一般情况下,你也可以采用excel来查询数据库数据. 但是人们通常认为sql会更加灵活和方便一些. sql学习的入门网站: http://w ...

  6. PHP学习笔记 - 入门篇(5)

    PHP学习笔记 - 入门篇(5) 语言结构语句 顺序结构 eg: <?php $shoesPrice = 49; //鞋子单价 $shoesNum = 1; //鞋子数量 $shoesMoney ...

  7. PHP学习笔记 - 入门篇(4)

    PHP学习笔记 - 入门篇(4) 什么是运算符 PHP运算符一般分为算术运算符.赋值运算符.比较运算符.三元运算符.逻辑运算符.字符串连接运算符.错误控制运算符. PHP中的算术运算符 算术运算符主要 ...

  8. PHP学习笔记 - 入门篇(3)

    PHP学习笔记 - 入门篇(3) 常量 什么是常量 什么是常量?常量可以理解为值不变的量(如圆周率):或者是常量值被定义后,在脚本的其他任何地方都不可以被改变.PHP中的常量分为自定义常量和系统常量 ...

  9. PHP学习笔记--入门篇

    PHP学习笔记--入门篇 一.Echo语句 1.格式 echo是PHP中的输出语句,可以把字符串输出(字符串用双引号括起来) 如下代码 <?php echo "Hello world! ...

随机推荐

  1. 9. JEB 1.5插件编写一

    2.0之后好像新增加了很多API,有所区别 本文采用Java+Eclipse作为开发环境 插件支持的语言  文档: jeb-1.5/doc/index.html 一.Hello World工程 1. ...

  2. sed使用的并不是完全的正则表达式

    经过实验发现,命令sed 's/pattern/replacement/' file中,pattern使用的并不是完全的正则表达式,而如果想使用正则表达式,需要使用sed命令的 -r 选项: sed ...

  3. IntelliJ IDEA 导入web项目的一些配置

  4. 比较NHibernate和Entity Framework

    葡萄牙的一位开发者 Ricardo Peres 最近发布了一篇文章,以看起来无偏见的形式对领先的两种 .NET ORM:NHibernate 和 Entity Framework 进行了比较. 我们建 ...

  5. centos vim配置高亮语法和格式化粘贴

    centos vim配置高亮语法和格式化粘贴 设置vim别名和高亮grep词语 echo -e "\nalias vi=vim\nalias grep='grep --color'\n&qu ...

  6. svn迁移到git仓库并保留commit历史记录

    svn迁移到git仓库并保留commit历史记录 最近在做svn迁移到gitlab,由于之前一直是由svn做版本控制.最简单的方式是将svn的内容export出来,然后添加到gitlab即可.但是,如 ...

  7. response content-type json

    2015年11月3日 15:47:43 百度知道:ajax开发中在请求服务器端的响应时, 对于每一种返回类型 规范的做法是要在服务端指定response的contentType 常遇到下面的几种情况: ...

  8. 7.js模式-装饰者模式

    1. 装饰者模式 给对象动态增加职责的方式称为装饰者模式. Function.prototype.before = function(beforefn){ var _self = this; retu ...

  9. MySQL\MariaDB 多线程复制初探

    背景: MariaDB 在10.0.5就已经支持了并行复制的功能,即从库多线程复制的功能.MySQL最先在5.6.3中支持.目前暂时没有用MySQL5.6的版本,故暂时只对MariaDB进行一些说明, ...

  10. MySQL中无GROUP BY直接HAVING的问题【转】

    本文来自网址: http://www.penglixun.com/tech/database/having_without_groupby_in_mysql.html 今天有同学给我反应,有一张表,i ...