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 - 登陆成功

原文地址:https://www.cnblogs.com/heben/p/5387370.html