我正在製作命令行工具多次訪問網站。我一次使用多個線程訪問頁面,每個線程使用循環重複訪問網站。工具工作正常,並根據需要訪問網站,但唯一的問題是面臨打開網站並在幾分鐘後關閉它。所以每次訪問的會話持續時間限制在3到4秒。我需要增加此會話持續時間至少60秒。以下是我的代碼。HtmlUnit WebClient Session Duration
package directUrl;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
public class ThreadDirectUrl extends Thread {
private String url;
private String paramUserAgent;
private String paramReferer;
private int loopSize;
public ThreadDirectUrl(String url, String paramUserAgent, String paramReferer, int loopSize) {
this.url = url;
this.paramUserAgent = paramUserAgent;
this.paramReferer = paramReferer;
this.loopSize = loopSize;
}
public void run() {
String userAgent = new String();
// Get User Agent
if (paramUserAgent.equals("1")) {
userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Firefox/17.0";
} else if (paramUserAgent.equals("2")) {
userAgent = "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
} else if (paramUserAgent.equals("3")) {
userAgent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
} else if (paramUserAgent.equals("4")) {
userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1";
}
BrowserVersion bv = new BrowserVersion("Netscape", "Version", userAgent, 0);
try {
URL openUrl = new URL(url);
for (int i = 1; i <= loopSize; i++) {
WebClient webClient = new WebClient(bv);
webClient.addRequestHeader("Accept-Encoding", "compress, gzip");
webClient.addRequestHeader("Referer", paramReferer);
webClient.getOptions().setPrintContentOnFailingStatusCode(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setPopupBlockerEnabled(true);
webClient.getOptions().setMaxInMemory(3);
webClient.getPage(openUrl);
System.out.println(Thread.currentThread().getName() + "----" + i + "----\nSuccess!\nUser Agent: "
+ bv.getUserAgent() + "\n\n");
Thread.sleep(60000);
webClient.getCurrentWindow().getJobManager().removeAllJobs();
webClient.close();
}
System.out.println(Thread.currentThread().getName() + "COMPLETED");
} catch (FailingHttpStatusCodeException e) {
System.out.println("Error!");
} catch (MalformedURLException e) {
System.out.println("Error - Use URL with \"http://\" or \"https://\"!");
} catch (IOException e) {
System.out.println("Error!");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error!");
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + "Interrupted");
} finally {
System.gc();
}
}
}
主要類是如下
package directUrl;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DirectUrl {
public static void main(String[] args) {
// Production Variables
String url = args[0]; // URL
String paramUserAgent = args[1]; // User Agent Choice
String paramReferer = args[2]; // Referrer URL
int loopSize = Integer.parseInt(args[3]); // Loop Size
int threadSize = Integer.parseInt(args[4]); // Counts of threads
Logger logger = Logger.getLogger("");
logger.setLevel(Level.OFF);
// Create Multiple Threads
ThreadDirectUrl aThread;
for (int i = 1; i <= threadSize; i++) {
aThread = new ThreadDirectUrl(url, paramUserAgent, paramReferer, loopSize);
aThread.setName("thread" + i);
aThread.start();
}
}
}
在類ThreadDirectUrl,我使用;
Thread.sleep(60000);
getPage()方法但它不工作。請建議。
什麼錯誤,你的睡眠後''弄,你可以通過'LogManager.getLogger(「org.apache.http.wire」),參見頭餅乾setLevel(ORG .apache.log4j.Level.ALL);'請發佈示例URL –
@AhmedAshour沒有錯誤。 –
那麼,你期望什麼?你得到一個頁面(隱含會話),接下來應該做什麼?什麼'不工作'。 –