⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gootripvisitorthread.java

📁 自己写的一个通过配置的多个代理服务器刷新某网页的一个java应用。可以经过改造应用于自动刷点击量
💻 JAVA
字号:
package com.gootrip.visitor.thread;


import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.TimerTask;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.xml.XmlBeanFactory;

public class GootripVisitorThread extends TimerTask {
	private Config config;

	private static final Logger log = Logger.getRootLogger();

	public GootripVisitorThread(Config config, XmlBeanFactory factory) {
		this.config = config;
	}

	public void run() {
	
		// Create an HttpClient with the MultiThreadedHttpConnectionManager.
		// This connection manager must be used if more than one thread will
		// be using the HttpClient.
		HttpClient httpClient = new HttpClient(
				new MultiThreadedHttpConnectionManager());

		// create an array of URIs to perform GETs on
		int threadcount = config.getThreadcount();
		String host = config.getHost();
		String[] urisToGet = new String[threadcount];
		for (int k = 0; k < threadcount; k++) {
			urisToGet[k] = host;
		}

		// create a thread for each URI
		GetThread[] threads = new GetThread[urisToGet.length];
		for (int i = 0; i < threads.length; i++) {
			GetMethod get = new GetMethod(urisToGet[i]);
			get.setFollowRedirects(false);
			threads[i] = new GetThread(httpClient, get, i + 1, getProxy());
		}

		// start the threads
		for (int j = 0; j < threads.length; j++) {
			threads[j].start();
		}
		log.info("<timertask>启动" + threadcount + "访问线程完毕");
	}

	private Proxy getProxy() {
		// TODO Auto-generated method stub
		Map proxys = config.getProxys();
		
		Iterator iter = proxys.keySet().iterator();
		String proxy = null;
		int port = 0;
		
		//random index gen..
	    Random random = new Random();
		int randIdx = (Math.abs(random.nextInt()) % proxys.size()); 
		log.info("randIdx:"+randIdx);
		//get the special proxy
		int i = 0;
		while (iter.hasNext()) {
			if(i >= randIdx) {
				proxy = (String) iter.next();
				port = Integer.parseInt((String)proxys.get(proxy));
				return new Proxy(proxy,port);
			}
			if(i == 0){
				// keep the first proxy as default
				proxy = (String) iter.next();
				log.info("OK:"+(String)proxys.get(proxy));
				port = Integer.parseInt((String)proxys.get(proxy));
			}
			i++;
		}
		return new Proxy(proxy,port);
	}

	/**
	 * A thread that performs a GET.
	 */
	static class GetThread extends Thread {

		private HttpClient httpClient;

		private GetMethod method;

		private int id;

		private Proxy proxy;

		public GetThread(HttpClient httpClient, GetMethod method, int id,
				Proxy proxy) {
			this.httpClient = httpClient;
			this.method = method;
			this.id = id;
			this.proxy = proxy;
		}

		/**
		 * Executes the GetMethod and prints some satus information.
		 */
		public void run() {

			try {

				log.info(id + " - about to get something from "
						+ method.getURI());
				// set the different IP,not succeed in jsp,but ok in php.
				/*
				 * method.addRequestHeader("Remote-Addr", "0");
				 * method.addRequestHeader("User-Agent", "my IE 2.0");
				 * method.addRequestHeader("Client-Ip", "202.206.0.48");
				 * method.addRequestHeader("X-FORWARDED-For", "202.206.0.49");
				 */
				// HostConfiguration tmp = httpClient.getHostConfiguration();
				// log.info("IP:"+ip);
				// tmp.setLocalAddress(InetAddress.getByName(ip));
				// log.error("error");
				// httpClient.setHostConfiguration(tmp);
				// httpClient.getHttpConnectionManager().
				// execute the method
				httpClient.getHostConfiguration().setProxy(proxy.getIp(), proxy.getPort());
				httpClient.executeMethod(method);

				log.info(id + " - get executed");

				// get the response body as an array of bytes
				byte[] bytes = method.getResponseBody();
				log.info(id + " - " + bytes.length + " bytes read");

				log.info(method.getResponseBodyAsString());

			} catch (Exception e) {
				log.error(id + " - error: " + e);
			} finally {
				// always release the connection after we're done
				method.releaseConnection();
				log.info(id + " - connection released");
			}
		}

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -