HTTPCLIENT 学习 (1) 入门
早就如雷贯耳它的大名,却一直不曾相见,昨天下载下来,今天终于测试了一把,用的官网的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) 入门的更多相关文章
- HttpClient学习整理
HttpClient简介HttpClient 功能介绍 1. 读取网页(HTTP/HTTPS)内容 2.使用POST方式提交数据(httpClient3) 3. 处理页面重定向 ...
- HttpClient 学习整理【转】
转自 http://www.blogjava.net/Alpha/archive/2007/01/22/95216.html HttpClient 是我最近想研究的东西,以前想过的一些应用没能有很好的 ...
- HttpClient 学习整理 (转)
source:http://www.blogjava.net/Alpha/archive/2007/01/22/95216.html HttpClient 是我最近想研究的东西,以前想过的一些应用没能 ...
- Python学习--01入门
Python学习--01入门 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.和PHP一样,它是后端开发语言. 如果有C语言.PHP语言.JAVA语言等其中一种语言的基础,学习Py ...
- [IT学习]sql 入门及实例
sql 是一种数据库查询语言,可以让你很快的查询到数据.其实一般情况下,你也可以采用excel来查询数据库数据. 但是人们通常认为sql会更加灵活和方便一些. sql学习的入门网站: http://w ...
- PHP学习笔记 - 入门篇(5)
PHP学习笔记 - 入门篇(5) 语言结构语句 顺序结构 eg: <?php $shoesPrice = 49; //鞋子单价 $shoesNum = 1; //鞋子数量 $shoesMoney ...
- PHP学习笔记 - 入门篇(4)
PHP学习笔记 - 入门篇(4) 什么是运算符 PHP运算符一般分为算术运算符.赋值运算符.比较运算符.三元运算符.逻辑运算符.字符串连接运算符.错误控制运算符. PHP中的算术运算符 算术运算符主要 ...
- PHP学习笔记 - 入门篇(3)
PHP学习笔记 - 入门篇(3) 常量 什么是常量 什么是常量?常量可以理解为值不变的量(如圆周率):或者是常量值被定义后,在脚本的其他任何地方都不可以被改变.PHP中的常量分为自定义常量和系统常量 ...
- PHP学习笔记--入门篇
PHP学习笔记--入门篇 一.Echo语句 1.格式 echo是PHP中的输出语句,可以把字符串输出(字符串用双引号括起来) 如下代码 <?php echo "Hello world! ...
随机推荐
- 7.6--找过点最多的直线(CC150)
直接两个点确定一条直线.然后两两组合,再写一个看过多少个点的函数.一直更新max就行. import java.util.Arrays; public class Solution { public ...
- 将 JAR 转为 EXE – JSMOOTH 的使用教程(第二期)(转载)
http://www.iteknical.com/convert-jar-to-exe-phase-ii-jsmooth-use-tutorial/
- 强制QQ好友
tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=32595667&website=www.oicq ...
- [20160725]MyComparableTest
知识点: 1.Collections的使用. 2.自定义类泛型的使用. 3.自定义类,toString();equals();hashCode()方法的重写. import java.util.*; ...
- 转: Oracle表空间查询
1.查询数据库中的表空间名称 1)查询所有表空间 select tablespace_name from dba_tablespaces; select tablespace_name from us ...
- swfit中的同步锁
swfit 中 objective-c 中的@syncronized 这个东西不能用了,应该用 objc_sync_enter(self) 代码 objc_sync_exit(self) 代替!
- angular.js初探
2015年7月27日 22:26:35 星期一 用在我论坛里的小栗子: 先列出来一级回帖, 点击帖子前边的"查看回复"按钮无刷新的去请求该帖子的所有回复 首先要引用js文件, 我这 ...
- tortoiseSVN svn+ssh
2015年6月28日 11:45:10 星期日 今天实验用小海龟svn客户端的svn+ssh协议去链接版本库, 期望会快一点儿 首先在设置里 记着将连接ssh用的用户名和密码一块儿写到输入框中: -l ...
- DataStage
parallel job shell调用:dsjob ./dsjob -run -mode NORMAL -paramfile xxx.param <PROJECT> <JOB> ...
- 3.kvm的基本管理
1.查看KVM虚拟机配置文件 #KVM虚拟机默认配置文件位置 [root@kvm qemu]# pwd /etc/libvirt/qemu [root@kvm qemu]# ll total 12 # ...