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";
Simply use a thread pool
private static Executorservice es = Executors.newfixedthreadpool (30);
public static void Main (string[] args) {
Simply use a thread pool
Executorservice es = Executors.newfixedthreadpool (30);
The number of threads should be based on your own network. My home is 10M net so 20 is basically the upper limit.
for (int i=0;i<1;i++) {
Runnable r = new Runnable () {
public void Run () {
Refreshblog ();
}
};
Es.submit (R);
//refreshblog ();
}
}
/**
* Refreshblog This method is the corresponding request
* @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 ");
Actually, I don't know much about it here. I just know about the cookie generation strategy, but because CSDN will have a good sense of what the cookie is. Must be added. Browser_compatibility
Getmethod.getparams (). Setcookiepolicy (Org.apache.commons.httpclient.cookie.CookiePolicy.BROWSER_COMPATIBILITY) ;
while (true) {
try {
This sentence can cause blocking because you want the network to initiate the request.
int statusCode = Httpclient.executemethod (GetMethod);
if (statusCode! = HTTPSTATUS.SC_OK) {
System.out.print ("failure:" + getmethod.getstatusline ());
Thread thread = Thread.CurrentThread ();
Thread.Sleep (1500);//pause for 1.5 seconds until the program resumes execution
Return
}
SYSTEM.OUT.PRINTLN ("You have refreshed:" +number++);
} catch (Exception e) {
System.out.print ("Please check the network address!") ");
} finally {
Getmethod.releaseconnection ();
}
}
}
/* public static void Go (GetMethod getmethod) {
HttpClient httpclient=new HttpClient ();
Laziness uses a dead loop.
while (true) {
try {
This sentence can cause blocking because you want the network to initiate the request.
int statusCode = Httpclient.executemethod (GetMethod);
if (statusCode! = HTTPSTATUS.SC_OK) {
System.out.print ("failure:" + getmethod.getstatusline ());
Thread thread = Thread.CurrentThread ();
Thread.Sleep (1500);//pause for 1.5 seconds until the program resumes execution
}
SYSTEM.OUT.PRINTLN ("You have refreshed:" +number++);
} catch (Exception e) {
System.out.print ("Please check the network address!") ");
} finally {
Getmethod.releaseconnection ();
}
}
}*/
}
Several jar packages required: http://download.csdn.net/detail/jiazhipeng12/8458661
This article is from the "Jia Xiaoxian" blog, make sure to keep this source http://hackerxian.blog.51cto.com/9240575/1615425
Refresh 51cto or CSDN traffic