使用代理做的投票机_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 使用代理做的投票机

使用代理做的投票机

 2012/6/29 16:37:12  zhengjie_dna  程序员俱乐部  我要评论(0)
  • 摘要:packagetest;importjava.io.BufferedInputStream;importjava.io.BufferedReader;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.IOException;importjava.net.InetSocketAddress;importjava.net.Proxy;importjava.net.URL;importjava.net
  • 标签:使用 代理
   
package test;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <b>类名称:</b>HttpConnet<br/> <b>类描述:</b><br/> <b>创建人:</b>ZhengJie<br/>
 * <b>修改人:</b>ZhengJie<br/> <b>修改时间:</b>2012-5-25 下午05:42:37<br/> <b>修改备注:</b><br/>
 * 
 * @version 1.2.0<br/>
 */
public class HttpConnet {
	static int runCount=0;
	public static void main(String[] args) {
//		System.getProperties().setProperty("http.proxyHost", "140.224.122.182");
//		System.getProperties().setProperty("http.proxyPort", "3128");
		// System.getProperties().put( "proxySet", "true" );
		// System.getProperties().put( "proxyHost", "140.224.122.182" );
		// System.getProperties().put( "proxyPort", 8080);
		//"http://www.yorkfcwr.com/FHandler.ashx?id=427&key=do-vote&memberid=427&t=0.2389311243314296"
		//System.out.println(getHtml("http://www.ip138.com/ip2city.asp")); // 判断代理是否设置成功
		URL url= HttpConnet.class.getResource("2.txt");
		BufferedReader in=null;
		List<String> adds=new ArrayList<String>();
		try {
			in = new   BufferedReader(new   FileReader( url.getFile().substring(1)));
			String   line   =   null; 
			while((line   =   in.readLine())!=null) 
			{ 
			    String http=line.split(" ")[0];
			    //System.out.println(http);
			    adds.add(http);
			}
			ExecutorService pool = Executors.newFixedThreadPool(2);

			for (String string : adds) {
				String[] addArr= string.split(":");
				if(addArr==null || addArr.length<2)continue;
				System.out.println(string);
				String http=addArr[0];
				int port=Integer.parseInt(addArr[1]);
				HttpConnet.MyThread thread=new HttpConnet().new MyThread(http,port);
				pool.execute(thread);
			}
			pool.shutdown();
			
		}catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			 try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
        
       
	}
	public class MyThread extends Thread {
		String url;
		int port;
		public MyThread(String url ,int port) {
			this.url=url;
			this.port=port;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			threadEvent();
		}
		private  void threadEvent(){
			for (int i = 0; i < 9; i++) {
				try {			
					long date=new Date().getTime();
//					System.out.println(getHtml2("http://www.ip138.com/ip2city.asp")); // 判断代理是否设置成功
					String result=getHtml("http://www.yorkfcwr.com/FHandler.ashx?id=427&key=do-vote&memberid=427&t="+date,url,port);
					if(!"{\"result\":\"1\",\"msg\":\"投票成功!\"}".equals(result)){
						break;
					}else{
						runCount++;
						System.out.println("runCount:"+runCount);
					}
					Thread.sleep(1000);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
					break;
				}
			}
		}
	}
	
	
	
	
	private static String getHtml(String address,String http,int port) throws Exception {

		StringBuffer html = new StringBuffer();

		String result = null;

		try {

			URL url = new URL(address);
			InetSocketAddress addr = new InetSocketAddress(http,port);             
			Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http 代理   
			URLConnection conn = url.openConnection(proxy);
//			URLConnection conn = url.openConnection();

			conn.setRequestProperty(
							"User-Agent",
							"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)");
			
			BufferedInputStream in = new BufferedInputStream(conn
					.getInputStream());

			try {

				String inputLine;

				byte[] buf = new byte[4096];

				int bytesRead = 0;

				while (bytesRead >= 0) {

					inputLine = new String(buf, 0, bytesRead, "ISO-8859-1");

					html.append(inputLine);

					bytesRead = in.read(buf);

					inputLine = null;

				}

				buf = null;

			} finally {

				in.close();

				conn = null;

				url = null;

			}

			result = new String(html.toString().trim().getBytes("ISO-8859-1"),
					"UTF-8").toLowerCase();

		} catch (Exception e) {

			//e.printStackTrace();
			throw e;
		}

		html = null;

		return result;

	}

	private static String getHtml2(String address) {

		StringBuffer html = new StringBuffer();

		String result = null;

		try {

			URL url = new URL(address);

			URLConnection conn = url.openConnection();

			conn
					.setRequestProperty(
							"User-Agent",
							"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)");

			BufferedInputStream in = new BufferedInputStream(conn
					.getInputStream());

			try {

				String inputLine;

				byte[] buf = new byte[4096];

				int bytesRead = 0;

				while (bytesRead >= 0) {

					inputLine = new String(buf, 0, bytesRead, "ISO-8859-1");

					html.append(inputLine);

					bytesRead = in.read(buf);

					inputLine = null;

				}

				buf = null;

			} finally {

				in.close();

				conn = null;

				url = null;

			}

			result = new String(html.toString().trim().getBytes("ISO-8859-1"),
					"gb2312").toLowerCase();

		} catch (Exception e) {

			e.printStackTrace();

			return null;

		}

		html = null;

		return result;

	}
}

  • 2.rar (12.8 KB)
  • 下载次数: 0
发表评论
用户名: 匿名