httpClient 操作

public class HttpClientFetch extends TestCase {  
  
    // 使用 GET 方式向后台递交请求  
    public void testFetch01() {  
        try {  
            // HttpClient主要负责执行请求,可以把它看做是一个浏览器  
            HttpClient httpclient = new DefaultHttpClient();  
            // 利用HTTP GET向服务器发起请求  
            HttpGet get = new HttpGet("http://empower.edtest.com:8080/");  
            // 获得服务器响应的的所有信息  
            HttpResponse response = httpclient.execute(get);  
            // 获得服务器响应回来的消息体(不包括HTTP HEAD)  
            HttpEntity entity = response.getEntity();  
            if (entity != null) {  
                InputStream is = entity.getContent();  
                // 将InputStream转换为Reader,并使用缓冲读取,提高效率,同时可以按行读取内容  
                BufferedReader br = new BufferedReader(new InputStreamReader(  
                        is, "UTF-8"));  
                String line = null;  
                while ((line = br.readLine()) != null) {  
                    System.out.println(line);  
                }  
                is.close();  
            }  
            // 释放所有的链接资源,一般在所有的请求处理完成之后,才需要释放  
            httpclient.getConnectionManager().shutdown();  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    // 自动获得响应的编码信息  
    public void testFetch02() {  
        try {  
            // HttpClient主要负责执行请求  
            HttpClient httpclient = new DefaultHttpClient();  
            // 利用HTTP GET向服务器发起请求  
            HttpGet get = new HttpGet("http://www.baidu.com/");// new  
                                                                // HttpGet("http://localhost:8080/cms");  
            // 获得服务器响应的的所有信息  
            HttpResponse response = httpclient.execute(get);  
            // 获得服务器响应回来的消息体(不包括HTTP HEAD)  
            HttpEntity entity = response.getEntity();  
            if (entity != null) {  
                // 获得响应的字符集编码信息  
                // 即获取HTTP HEAD的:Content-Type:text/html;charset=UTF-8中的字符集信息  
                String charset = EntityUtils.getContentCharSet(entity);  
                System.out.println("响应的字符集是:" + charset);  
                InputStream is = entity.getContent();  
                // 使用响应中的编码来解释响应的内容  
                BufferedReader br = new BufferedReader(new InputStreamReader(  
                        is, charset));  
                String line = null;  
                while ((line = br.readLine()) != null) {  
                    System.out.println(line);  
                }  
                is.close();  
            }  
            // 释放所有的链接资源,一般在所有的请求处理完成之后,才需要释放  
            httpclient.getConnectionManager().shutdown();  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    // 设置代理服务器,访问网站  
    public void testFetch03() {  
        try {  
            // HttpClient主要负责执行请求  
            HttpClient httpclient = new DefaultHttpClient();  
            // 设置代理服务器  
            httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,  
                    new HttpHost("121.12.249.207", 3128));  
            // 利用HTTP GET向服务器发起请求  
            HttpGet get = new HttpGet("http://www.baidu.com/");// new  
                                                                // HttpGet("http://localhost:8080/cms");  
            // 获得服务器响应的的所有信息  
            HttpResponse response = httpclient.execute(get);  
            // 获得服务器响应回来的消息体(不包括HTTP HEAD)  
            HttpEntity entity = response.getEntity();  
            if (entity != null) {  
                // 获得响应的字符集编码信息  
                // 即获取HTTP HEAD的:Content-Type:text/html;charset=UTF-8中的字符集信息  
                String charset = EntityUtils.getContentCharSet(entity);  
                System.out.println("响应的字符集是:" + charset);  
                InputStream is = entity.getContent();  
                // 使用响应中的编码来解释响应的内容  
                BufferedReader br = new BufferedReader(new InputStreamReader(  
                        is, charset));  
                String line = null;  
                while ((line = br.readLine()) != null) {  
                    System.out.println(line);  
                }  
                is.close();  
            }  
            // 释放所有的链接资源,一般在所有的请求处理完成之后,才需要释放  
            httpclient.getConnectionManager().shutdown();  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    /** 
     * 获得重定向之后的网址信息 HttpClient 缺省情况下自动处理客户端重定向,即当你访问网页(比如A 网页)之后,假设被重定向到了 B 
     * 网页,那么,HttpClient 将自动返回B 网页的内容,无需再编程处理它!有时候我们可能想要知道A 网 页被重定向到了哪里,也就是取得B 
     * 网页的网址,那么可以通过下述例子获得: 
     */  
    public void testFetch04() {  
        try {  
            // HttpClient主要负责执行请求  
            HttpClient httpclient = new DefaultHttpClient();  
            HttpContext context = new BasicHttpContext();  
            // 利用HTTP GET向服务器发起请求  
            HttpGet get = new HttpGet(  
                    "http://localhost:8080/cms/backend/main.jsp");  
            // 获得服务器响应的的所有信息  
            HttpResponse response = httpclient.execute(get, context);  
            // 获得重定向之后的主机地址信息  
            HttpHost targetHost = (HttpHost) context  
                    .getAttribute(ExecutionContext.HTTP_TARGET_HOST);  
            System.out.println(targetHost); // http://localhost:8080  
            // 获得实际的请求对象的URI(即重定向之后的"/cms/backend/login.jsp")  
            HttpUriRequest actualRequest = (HttpUriRequest) context  
                    .getAttribute(ExecutionContext.HTTP_REQUEST);  
            System.out.println(actualRequest.getURI());  
            // 获得服务器响应回来的消息体(不包括HTTP HEAD)  
            HttpEntity entity = response.getEntity();  
            if (entity != null) {  
                // 获得响应的字符集编码信息  
                // 即获取HTTP HEAD的:Content-Type:text/html;charset=UTF-8中的字符集信息  
                String charset = EntityUtils.getContentCharSet(entity);  
                System.out.println("响应的字符集是:" + charset);  
                InputStream is = entity.getContent();  
                // 使用响应中的编码来解释响应的内容  
                BufferedReader br = new BufferedReader(new InputStreamReader(  
                        is, charset));  
                String line = null;  
                while ((line = br.readLine()) != null) {  
                    System.out.println(line);  
                }  
                is.close();  
            }  
            // 释放所有的链接资源,一般在所有的请求处理完成之后,才需要释放  
            httpclient.getConnectionManager().shutdown();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
    /* 
     * HttpContext,实际上是客户端用来在多次请求-响应的交互中,保持状态信息用的。假如我们在调用 httpclient.execute 
     * 方法的时候,将HttpContext 对象作为参数传给这个方法(请看上述例子),那么 HttpClient 
     * 将把请求-响应交互过程中的状态信息存储在HttpContext 中。 比如上面的例子中,HttpClient 
     * 把主机信息和真正的请求对象(所谓真正的请求对象,因为我们发出的是 main.jsp 
     * 的请求,但这个请求实际上被重定向到了login.jsp,所以真正的请求对象实际上是 
     * login.jsp)等信息(请参考文档说明)放到了HttpContext 中! 我们自己也可以利用HttpContext 
     * 来存放一些我们想要存放的其它信息,以便下次请求的时候,能够把这 些信息拿出来使用! 
     */  
  
    /* 
     * HttpClient 能够支持自动Cookie 处理。设想一个典型的场景:首先打开登录页面,然后输入用户名和密 
     * 码登录,然后访问那些只有登录之后才能访问的网页…… 如果我们用浏览器,因为浏览器可以将登录之后的会话信息用Cookie 
     * 存储在本地,所以,登录之后的每次 请求,都会自动向服务器发送Cookie 的信息,我们利用HttpClient,这些过程都全部可以自动化处理 了。 
     */  
    public void testFetch05() {  
        try {  
            // HttpClient主要负责执行请求  
            HttpClient httpclient = new DefaultHttpClient();  
            HttpContext context = new BasicHttpContext();  
            // 利用HTTP GET向服务器发起请求,  
            HttpGet get = new HttpGet("http://localhost:8080/cms/backend/login.jsp");  
            // 获得服务器响应的的所有信息  
            HttpResponse response = httpclient.execute(get, context);  
            // 获得服务器响应回来的消息体(不包括HTTP HEAD)  
            HttpEntity entity = response.getEntity();  
            String charset = null;  
            if (entity != null) {  
                // 获得响应的字符集编码信息  
                // 即获取HTTP HEAD的:Content-Type:text/html;charset=UTF-8中的字符集信息  
                charset = EntityUtils.getContentCharSet(entity);  
                System.out.println("响应的字符集是:" + charset);  
                InputStream is = entity.getContent();  
                // 使用响应中的编码来解释响应的内容  
                BufferedReader br = new BufferedReader(new InputStreamReader(  
                        is, charset));  
                String line = null;  
                while ((line = br.readLine()) != null) {  
                    System.out.println(line);  
                }  
                is.close();  
            }  
            // ************* 执行登录请求 ********************//  
            HttpPost post = new HttpPost("http://localhost:8080/cms/backend/LoginServlet");  
            // 添加POST参数  
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
            nvps.add(new BasicNameValuePair("username", "admin"));  
            nvps.add(new BasicNameValuePair("password", "admin"));  
            //浏览器向服务器传递数据的时候,会在后台对数据先进行编码。  
            post.setEntity(new UrlEncodedFormEntity(nvps, charset));  
            response = httpclient.execute(post);  
            entity = response.getEntity();  
            if (entity != null) {  
                InputStream is = entity.getContent();  
                // 使用响应中的编码来解释响应的内容  
                BufferedReader br = new BufferedReader(new InputStreamReader(  
                        is, charset));  
                String line = null;  
                while ((line = br.readLine()) != null) {  
                    System.out.println(line);  
                }  
                is.close();  
            }  
            // ******************* 请求文章查询 ********************//  
            get = new HttpGet("http://localhost:8080/cms/backend/ArticleServlet");  
            //要保证这里的httpclient与之前的是同一个  
            response = httpclient.execute(get);  
            entity = response.getEntity();  
            if (entity != null) {  
                InputStream is = entity.getContent();  
                // 使用响应中的编码来解释响应的内容  
                BufferedReader br = new BufferedReader(new InputStreamReader(  
                        is, charset));  
                String line = null;  
                while ((line = br.readLine()) != null) {  
                    System.out.println(line);  
                }  
                is.close();  
            }  
            // 释放所有的链接资源,一般在所有的请求处理完成之后,才需要释放  
            httpclient.getConnectionManager().shutdown();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  


你可能感兴趣的:(httpClient 操作)