`
xiangxingchina
  • 浏览: 508473 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

HttpClient 模拟火狐

 
阅读更多
HttpClient是HttpComponents(简称为hc)项目其中的一部份,访问地址:http://hc.apache.org/



HttpClient是一个代码级的Http客户端工具,可以使用它模拟浏览器向Http服务器发送请求。使用HttpClient还需要HttpCore.后者包括Http请求与Http响应的代码封装。



Hello World
Java代码  收藏代码

    public final static void main(String[] args) throws Exception { 
        HttpClient httpclient = new DefaultHttpClient(); 
        try { 
            HttpGet httpget = new HttpGet("http://www.apache.org/"); 
            System.out.println("executing request " + httpget.getURI()); 
            HttpResponse response = httpclient.execute(httpget); 
            HttpEntity entity = response.getEntity(); 
     
            System.out.println("----------------------------------------"); 
            System.out.println(response.getStatusLine()); 
            if (entity != null) { 
                System.out.println("Response content length: " + entity.getContentLength()); 
            } 
            System.out.println("----------------------------------------"); 
     
            InputStream inSm = entity.getContent(); 
            Scanner inScn = new Scanner(inSm); 
            while (inScn.hasNextLine()) { 
                System.out.println(inScn.nextLine()); 
            } 
            // Do not feel like reading the response body 
            // Call abort on the request object 
            httpget.abort(); 
        } finally { 
            // When HttpClient instance is no longer needed, 
            // shut down the connection manager to ensure 
            // immediate deallocation of all system resources 
            httpclient.getConnectionManager().shutdown(); 
        } 
    } 



[点击查看原始大小图片]

使用HC模拟Firefox的请求

1:分析请求包中这六个头信息



2:代码
Java代码  收藏代码

    /**
     * @param args
     * @throws IOException
     */ 
    public static void main(String[] args) { 
        HttpClient httpClient = new DefaultHttpClient(); 
        try { 
            HttpGet httpget = new HttpGet("http://www.iteye.com"); 
     
            httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
            httpget.setHeader("Accept-Language", "zh-cn,zh;q=0.5"); 
            httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1)"); 
            httpget.setHeader("Accept-Encoding", "gzip, deflate"); 
            httpget.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"); 
            httpget.setHeader("Host", "www.iteye.com"); 
            httpget.setHeader("Connection", "Keep-Alive"); 
     
            HttpResponse response = httpClient.execute(httpget); 
            HttpEntity entity = response.getEntity(); 
     
            System.out.println("----------------------------------------"); 
            System.out.println(response.getStatusLine()); 
            if (entity != null) { 
                System.out.println("Response content length: " + entity.getContentLength()); 
            } 
            System.out.println("----------------------------------------"); 
     
            InputStream inSm = entity.getContent(); 
            Scanner inScn = new Scanner(inSm); 
            while (inScn.hasNextLine()) { 
                System.out.println(inScn.nextLine()); 
            } 
            // Do not feel like reading the response body 
            // Call abort on the request object 
            httpget.abort(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            // When HttpClient instance is no longer needed, 
            // shut down the connection manager to ensure 
            // immediate deallocation of all system resources 
            httpClient.getConnectionManager().shutdown(); 
        } 
    } 


3:效果

[点击查看原始大小图片]
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics