标签:number import public private
package xian;
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.methods.GetMethod;
public class RefreshBlog {
private static volatile int number = 0;
private static final String url ="http://hackerxian.blog.51cto.com/9240575/1615425";
//private static final String url ="http://blog.csdn.net/jiazhipeng12/article/details/43731675";
//简单的用一个线程池
private static ExecutorService es = Executors.newFixedThreadPool(30);
public static void main( String[] args ){
//简单的用一个线程池
// ExecutorService es = Executors.newFixedThreadPool(30);
//线程的个数应该根据自家的网络来定.我家是10M网所以20个基本上就是上限了.
for(int i=0;i<1;i++){
Runnable r = new Runnable() {
public void run() {
refreshBlog();
}
};
es.submit(r);
//refreshBlog();
}
}
/**
* refreshBlog 这个方法是相应请求
* @author heinz_ho
* @return void
*/
public static void refreshBlog(){
HttpClient httpClient=new HttpClient();
GetMethod getMethod = new GetMethod(url);
//csdn
//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");
//51cto
getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36 LBBROWSER");
//其实这里我也不太懂.我只是知道cookie的生成策略,但是因为 csdn会判断力过来的cookie是啥.必须加. BROWSER_COMPATIBILITY
getMethod.getParams().setCookiePolicy(org.apache.commons.httpclient.cookie.CookiePolicy.BROWSER_COMPATIBILITY);
while(true) {
try {
//这句话会造成阻塞因为想网络发起请求.
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.out.print("失败:" + getMethod.getStatusLine());
Thread thread = Thread.currentThread();
thread.sleep(1500);//暂停1.5秒后程序继续执行
//return;
}
System.out.println("你已经刷新了:"+number++);
} catch (Exception e) {
System.out.print("请检查网络地址!");
} finally {
getMethod.releaseConnection();
}
}
}
/* public static void go(GetMethod getMethod){
HttpClient httpClient=new HttpClient();
//偷懒就用了一个死循环
while(true) {
try {
//这句话会造成阻塞因为想网络发起请求.
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.out.print("失败:" + getMethod.getStatusLine());
Thread thread = Thread.currentThread();
thread.sleep(1500);//暂停1.5秒后程序继续执行
}
System.out.println("你已经刷新了:"+number++);
} catch (Exception e) {
System.out.print("请检查网络地址!");
} finally {
getMethod.releaseConnection();
}
}
}*/
}
需要的几个jar包:http://download.csdn.net/detail/jiazhipeng12/8458661
本文出自 “贾小仙” 博客,请务必保留此出处http://hackerxian.blog.51cto.com/9240575/1615425
标签:number import public private
原文地址:http://hackerxian.blog.51cto.com/9240575/1615425