站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题通过代理服务器访问url

通过代理服务器访问url

摘要: 通过代理服务器访问url

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="435" height="86" align="left" valign="top"> 这个程序用于模拟来自不同IP的用户访问某个URL
首先从一个代理服务器地址公布网站获得代理服务器列表,然后用随机的代理服务器为出口访问指定的URL
import java.io.*;

import java.net.*;

import java.util.*;

import java.util.regex.*;

public class AutoClick

{

    public static int count = 0;

    public static List ipList = new ArrayList();


</td> <td width="303" valign="top"> </td> </tr> </table>

public static void main(String[] args) throws Exception

    {

        String defurl="http://www.baidu.com";

        // 从以下网址获得代理服务器ip列表
URL url = new URL("http://www.cemsg.com/proxy/"); if (Math.random() > 0.5) { url = new URL("http://www.cemsg.com/proxy/2.htm"); } System.out.println("Use proxy list " + url); URLConnection conn = url.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); String str = ""; StringBuffer sbuf = new StringBuffer(); while ((str = br.readLine()) != null) { sbuf.append(str); } str = sbuf.toString(); // 从HTML中筛出代理ip和port信息 Pattern p = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:\\d{2,4}"); Matcher m = p.matcher(str); while (m.find()) { ipList.add(str.substring(m.start(), m.end())); } // 开一个线程用随机的代理地址访问指定的url new Thread(new ClickThread(defurl)).start(); } } class ClickThread implements Runnable { public ClickThread(String urlAddr) { this.urlAddr = urlAddr; } String proxyIP; String proxyPort; String urlAddr; public void run() { while (true) { try { String[] ipInfos = AutoClick.ipList.get((int) (Math.random() * AutoClick.ipList.size())).toString().split(":"); System.setProperty("http.proxyHost", ipInfos[0]); System.setProperty("http.proxyPort", ipInfos[1]); URL url = new URL(urlAddr); URLConnection conn = url.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); int rc = 0; while (br.readLine() != null) { rc++; } System.out.println("read " + rc); br.close(); AutoClick.count++; System.out.println(ipInfos[0] + ":" + ipInfos[1] + " click " + urlAddr + " " + AutoClick.count + " times"); } catch (Exception e) { } try { Thread.sleep(100); } catch (Exception e) { } } } }

C:\java>java AutoClick
Use proxy list http://www.cemsg.com/proxy/
read 16
213.186.49.115:3128 click http://www.baidu.com 1 times
read 16
200.124.234.211:8080 click http://www.baidu.com 2 times
read 16
200.216.193.115:3128 click http://www.baidu.com 3 times
read 16
217.199.172.146:80 click http://www.baidu.com 4 times
read 16
165.228.132.11:80 click http://www.baidu.com 5 times
read 16
80.188.28.6:3128 click http://www.baidu.com 6 times
read 16
64.89.241.155:3128 click http://www.baidu.com 7 times
read 16
200.124.234.211:8080 click http://www.baidu.com 8 times
read 16
202.143.159.8:8080 click http://www.baidu.com 9 times
read 16
195.175.37.175:80 click http://www.baidu.com 10 times
read 16
203.199.181.194:80 click http://www.baidu.com 11 times
read 16
203.115.10.30:80 click http://www.baidu.com 12 times
read 16
203.94.85.0:80 click http://www.baidu.com 13 times


</td> </tr> <tr>


↑返回目录
前一篇: Java程序性能测试
后一篇: 创建接受格式化文本的输入域