缘起于玩唱吧,因为唱吧好友少,访问量低,又不想加什么亲友团之类的,主要是太麻烦了,于是我就琢磨唱吧的访问机制,准备用java的httpclient库来进行刷访问量,想到动态IP重复使用就想到了用多线程来进行刷,但发现速度并没有增快,仍然是刷完一个网址刷下一个网址,这里刷的顺序是随机的。 主要让人伤脑筋的是那代理IP,去网上找了1000多个IP,有好多都是响应速度太慢了,以至于我设定了超时机制,超过一段时间的话就抛出异常并且忽略,只是速度太慢了,有待提高速度!!谁有好的改进的建议欢迎大家来分享,求大家指点。
web_look.java
import java.io.*; import java.util.*; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.params.HttpConnectionParams; public class Web_look { public static void main(String[] args) throws IOException { int count=0; Map<String,String> map =new HashMap<String,String>(); FileReader reader=new FileReader("c://IP.txt");//读取代理IP地址 BufferedReader br=new BufferedReader(reader); String str=null; while((str=br.readLine())!=null)//格式写成IP+端口 { String result[]=str.split("@"); String info[]=result[0].split(":"); map.put(info[0], info[1]); } Set<String> set=map.keySet(); Iterator<String> iter=set.iterator(); HttpClient client = new HttpClient(); //设置超时时间 client.setTimeout(10000); client.setConnectionTimeout(10000); //设置cookie管理策略 client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY); while(iter.hasNext())//不断的取出IP和端口 { String key=iter.next(); System.out.println(key+":"+map.get(key)); //用try-catch来忽略超时异常继续执行 try{ //设置代理服务器地址和端口 client.getHostConfiguration().setProxy(key,Integer.parseInt(map.get(key))); //这是用刷的是唱吧的访问量,亲测有效,只不过效率不是怎么高,因为代理服务器有快有慢,所以设置了超时机制。 //而且,貌似一个IP不能连续刷多首歌曲,所以就只能附带刷刷别的网页好了。 MyThread song1= new MyThread("http://changba.com/s/TKSPrlyJgkd4cUir8pGZkQ?code=Gt1bjDM0qnHCJdoHUPnwOOfDhUzYnTz-KfffnVr9zf7j8kkd7OlN9bo9NlHq5j6rFeHPZ8iGR3663XuAiDYOEcYV2ky5V8Bk",client); MyThread song2= new MyThread("http://changba.com/s/48SlsCcpHNiNXPji_VyU5Q?code=Gt1bjDM0qnHCJdoHUPnwOOfDhUzYnTz-KfffnVr9zf7j8kkd7OlN9bo9NlHq5j6rFeHPZ8iGR3663XuAiDYOEcYV2ky5V8Bk",client); // MyThread song3= new MyThread("http://changba.com/s/0GP_YQzqRJOJUHjDbRpPBg?code=Gt1bjDM0qnHCJdoHUPnwOOfDhUzYnTz-KfffnVr9zf7j8kkd7OlN9bo9NlHq5j6rFeHPZ8iGR3663XuAiDYOEcYV2ky5V8Bk",client); // MyThread song4= new MyThread("http://changba.com/s/XXCW9aOqzkiQW1ha1hSMvg?code=Gt1bjDM0qnHCJdoHUPnwOOfDhUzYnTz-KfffnVr9zf7j8kkd7OlN9bo9NlHq5j6rFeHPZ8iGR3663XuAiDYOEcYV2ky5V8Bk",client); // MyThread song5= new MyThread("http://changba.com/s/7HpyApA4gWXoh-V0usAMfw?code=Gt1bjDM0qnHCJdoHUPnwOOfDhUzYnTz-KfffnVr9zf7j8kkd7OlN9bo9NlHq5j6rFeHPZ8iGR3663XuAiDYOEcYV2ky5V8Bk",client); // MyThread song6= new MyThread("http://changba.com/s/8y6IvXu-8uwUrrT7uEtHEA?code=Gt1bjDM0qnHCJdoHUPnwOOfDhUzYnTz-KfffnVr9zf7j8kkd7OlN9bo9NlHq5j6rFeHPZ8iGR3663XuAiDYOEcYV2ky5V8Bk",client); // MyThread song7= new MyThread("http://changba.com/s/lyBc498kdr9N4DgR5lqFCg?code=Gt1bjDM0qnHCJdoHUPnwOOfDhUzYnTz-KfffnVr9zf7j8kkd7OlN9bo9NlHq5j6rFeHPZ8iGR3663XuAiDYOEcYV2ky5V8Bk",client); song1.run(); song2.run(); // song3.run(); // song4.run(); // song5.run(); // song6.run(); // song7.run(); //使用GET方法,如果服务器需要通过HTTPS连接,那只需要将下面URL中的http换成https //使用POST方法 //HttpMethod method = new PostMethod("http://changba.com/s/0yKWymXZXeGZcF0P3mM8Bg?code=Gt1bjDM0qnHCJdoHUPnwOOfDhUzYnTz-KfffnVr9zf7j8kkd7OlN9bo9NlHq5j6rFeHPZ8iGR3663XuAiDYOEcYV2ky5V8Bk"); //设置请求头,来模拟浏览器登陆,有些网站需要,有些网站不需要,看具体是什么网站了 // method.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;"); // method.setRequestHeader("Accept-Language", "zh-cn"); // method.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3"); // method.setRequestHeader("Accept-Charset","encoding"); // method.setRequestHeader("Keep-Alive", "300"); // method.setRequestHeader("Connection", "Keep-Alive"); // method.setRequestHeader("Cache-Control", "no-cache"); //client.executeMethod(method); //打印服务器返回的状态 //System.out.println(method.getStatusLine()); //打印返回的信息 //System.out.println(method.getResponseBodyAsString()); //释放连接,否则会报错recycle //method.releaseConnection(); }catch(Exception e) { e.printStackTrace(); } count++; System.out.println("第"+count+"个IP地址"); } } }
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.methods.PostMethod; public class MyThread { private String web_name; private HttpClient client; public MyThread(String web_name,HttpClient client) { this.web_name=web_name; this.client=client; } public void run() { HttpMethod method = new PostMethod(web_name); try { client.executeMethod(method); System.out.println(method.getStatusLine()); System.out.println(method.getResponseBodyAsString()); method.releaseConnection(); }catch(Exception e) { e.printStackTrace(); } } }
利用httpclient和多线程刷访问量代码,布布扣,bubuko.com
原文地址:http://blog.csdn.net/u012457196/article/details/37520033