HTTPCLIENT 学习 (1) 入门
2016-04-13 15:35
681 查看
早就如雷贯耳它的大名,却一直不曾相见,昨天下载下来,今天终于测试了一把,用的官网的QUICK START例子,来访问我自己以前开发过的WEB程序,因为这个网站恰好有一个写好的通过POST请求验证用户名密码进行登录的功能。
下面是QUICK START的代码:
运行之后,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 - 登陆成功
下面是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 - 登陆成功
相关文章推荐
- Linux 后台进程管理器 supervisor 安装配置(使用httpd来做被监控进程)
- 机器学习算法汇总:人工神经网络、深度学习及其它
- 使用Volley执行网络数据传输
- BPFP系列:优化电池使用时间:确定和监控网络连接状态
- 初探网络性能测试
- Ubuntu无法正常连接无线网络
- 仿iOS的网络请求的加载框
- Hyperscan 介绍与安装
- WARN (org.apache.commons.httpclient.HttpMethodBase:1511) - Cookie rejected 警告
- Linux网络编程--recv函数返回值详解
- 使用Jmeter进行http接口测试
- 使用SSH设置映射端口跳转,解决网络限制问题
- tomcat下配置https
- 通过SecureCRT设置代理,访问网络中web界面
- 从网上整理了一些关于比较时间大小和请求网络时间的代码,在这里展示一下
- tcp/ip三次握手
- 2015年ACM-ICPC亚洲区域赛合肥站网络预选赛H题——The Next (位运算)
- nagios网络流量监控(附pnp模板)
- 计算机网络学习笔记--数据链据层上的设备详解
- TCP/IP(十二)TCP的超时与重传