zoukankan      html  css  js  c++  java
  • 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 - 登陆成功

  • 相关阅读:
    ViewGroup和View
    十二、Android UI开发专题(转)
    十一、Android学习笔记_AsyncQueryHandler的应用
    十、Notepad++正则表达式使用
    九、Android学习笔记_ Android开发中使用软引用和弱引用防止内存溢出
    八、android jni 之C语言基础
    七、Android学习笔记_JNI hello world
    六、Android学习笔记_JNI_c调用java代码
    五、PackageManager获取版本号
    四、 Android之手机屏幕朝向
  • 原文地址:https://www.cnblogs.com/heben/p/5387370.html
Copyright © 2011-2022 走看看