zoukankan      html  css  js  c++  java
  • csdn博客刷点击率(java代码)

    此文为转载,亲测有效。

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.cookie.CookiePolicy;
    import org.apache.commons.httpclient.methods.GetMethod;
    
    
    public class Test {
    
        private static volatile int number = 0;  
        private static final String url ="http://blog.csdn.net/chinacsharper/article/details/17514923"; 
        
        /**
         * @param args
         */
        public static void main(String[] args) {
            //简单的用一个线程池  
            ExecutorService es = Executors.newFixedThreadPool(30);  
            //线程的个数应该根据自家的网络来定.我家是10M网所以20个基本上就是上限了.  
            for(int i=0;i<20;i++){  
                 Runnable r = new Runnable() {  
                        public void run() {  
                            refreshBlog();  
                        }  
                };  
                es.submit(r);  
            }  
        }
        
        /** 
            * refreshBlog 这个方法是相应请求 
            * @author heinz_ho 
            * @return void 
            */  
           public static  void refreshBlog(){  
             HttpClient httpClient = new HttpClient();    
             GetMethod getMethod = new GetMethod(url);   
             getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36 LBBROWSER");  
             //其实这里我也不太懂.我只是知道cookie的生成策略,但是因为 csdn会判断力过来的cookie是啥.必须加.  
             getMethod.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);  
             //偷懒就用了一个死循环  
             while(true) {    
                   try {  
                    //这句话会造成阻塞因为想网络发起请求.  
                       int statusCode = httpClient.executeMethod(getMethod);  
                       if (statusCode != HttpStatus.SC_OK) {    
                           System.out.print("失败:" + getMethod.getStatusLine());    
                       }    
                       System.out.println("你已经刷新了:"+number++);  
                   } catch (Exception e) {    
                       System.out.print("请检查网络地址!");    
                   } finally {    
                       getMethod.releaseConnection();    
                   }    
             }     
           }  
    
    }
  • 相关阅读:
    linux基础命令:alias
    linux基础命令:find
    Linux下which、whereis、locate命令的区别
    逆元知识普及(进阶篇) ——from Judge
    BZOJ 3620: 似乎在梦中见过的样子
    HDU contest808 ACM多校第7场 Problem
    P3203 [HNOI2010]弹飞绵羊 —— 懒标记?分块?LCT?...FAQ orz
    可持久化数组(线段树)[模板题]
    可持久化并(xian)查(duan)集(shu)
    主席树(静态)的轻松入门
  • 原文地址:https://www.cnblogs.com/guwei4037/p/5616184.html
Copyright © 2011-2022 走看看