用http协议请求使用代理ip

有这个一个需求,领导要求在网上给一个合作伙伴投票。投票系统做的不是很好有一些漏洞,作为程序猿的我们,果断开始了自动刷票。

1、首先按F12查到网页源代码,截取出来投票链接。

2、用程序模拟http get请求通过循环执行。

3、模拟执行的时候通过实践发现同一个ip在一定时间内只能投10次,故此需要用到代理ip

下面看程序:

public class Toupiao {
	public static void main(String args[])
	{
		new Thread(){
			public void run() {
				while(true){
					try{
						doGet();
						Random r = new Random();
						int d = r.nextInt(20);
						Thread.sleep(d*2000);
					}catch(Exception e){
						e.printStackTrace();
					}
				}
			};
		}.start();
	}
	
	public static void doGet(){

		 StringBuffer sb = new StringBuffer();
		 //创建HttpClient实例
		 HttpClient client = getHttpClient();
		 //创建httpGet
		 HttpGet httpGet = new HttpGet("http://rank.cn-healthcare.com/vote/judge/3/5678c9c370617c24df5ab8bd");
		 //执行
		 try {
		  HttpResponse response = client.execute(httpGet);
		  
		  HttpEntity entry = response.getEntity();
		  
		  if(entry != null)
		  {
		   InputStreamReader is = new InputStreamReader(entry.getContent());
		   BufferedReader br = new BufferedReader(is);
		   String str = null;
		   while((str = br.readLine()) != null)
		   {
		    sb.append(str.trim());
		   }
		   br.close();
		  }
		  
		 } catch (ClientProtocolException e) {
		  // TODO Auto-generated catch block
		  e.printStackTrace();
		 } catch (IOException e) {
		  // TODO Auto-generated catch block
		  e.printStackTrace();
		 }
		 System.out.println(sb.toString());
		
	}

	//设置代理

	public static HttpClient getHttpClient() {

	DefaultHttpClient httpClient = new DefaultHttpClient();
	 String proxyHost = "58.67.159.50"; //此处是代理ip
	 int proxyPort = 80; //代理端口
	 HttpHost proxy = new HttpHost(proxyHost,proxyPort);
	 httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
	 return httpClient;
	}
}

 还有一个问题就是找代理ip

http://www.youdaili.net/

此网站有许多代理ip,有些可以用有些不可以用需要自己去尝试。

你可以先在自己的电脑上设置下代理ip试试看能不能访问。如下:

用http协议请求使用代理ip_第1张图片

这样我现在本机的ip地址就是泰国的了,同时有些ip不能用则上网是上不了的。这样也可以判断代理ip的可用性。这里用代理ip也可以访问国外的网站但是速度和稳定性就。。。。。。。。

本篇完


你可能感兴趣的:(用http协议请求使用代理ip)