HttpComponents实战

来源:互联网 发布:同账号 苹果 软件 编辑:程序博客网 时间:2024/05/16 19:13

注册某论坛,对新人限制非常多,处处不便。

看到可以通过邀请注册快速活动经验,动起主意,想通过程序快速注册多个帐号来获取经验。

试验发现,单ip720小时内只能注册一个帐号,虽说可以不断重启路由来重新获得ip但还嫌麻烦。

想到平常用的 GAE 翻墙,google的代理服务器肯定很多,一定有很多不同的ip。


开始尝试 httpcomponents通过GAE代理连接 ip138,获取到美国ip则说明代理成功。

1.本地 GAE代理服务开好(具体方法搜“GAE翻@墙”)

2.代码

[java] view plaincopy
  1. package com.zhh.examples.client;  
  2.   
  3. import org.apache.http.HttpEntity;  
  4. import org.apache.http.HttpHost;  
  5. import org.apache.http.HttpResponse;  
  6. import org.apache.http.auth.AuthScope;  
  7. import org.apache.http.auth.UsernamePasswordCredentials;  
  8. import org.apache.http.client.methods.HttpPost;  
  9. import org.apache.http.conn.params.ConnRoutePNames;  
  10. import org.apache.http.impl.client.DefaultHttpClient;  
  11. import org.apache.http.util.EntityUtils;  
  12.   
  13. public class ProxyDemo {  
  14.   
  15.     public static void main(String[] args) throws Exception {  
  16.   
  17.         String proxyUrl = "127.0.0.1";  
  18.         int proxyPort = 8087;  
  19.         String url = "http://iframe.ip138.com/city.asp";  
  20.   
  21.         DefaultHttpClient client = new DefaultHttpClient();  
  22.         client.getCredentialsProvider().setCredentials(  
  23.                                 new AuthScope(proxyUrl, proxyPort),  
  24.                                 new UsernamePasswordCredentials(""""));  
  25.         HttpHost proxy = new HttpHost(proxyUrl, proxyPort);  
  26.         client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);  
  27.         // 设置代理结束  
  28.           
  29.         HttpPost post = new HttpPost(url);  
  30.         HttpResponse response = client.execute(post);  
  31.           
  32.         // 打印出状态码  
  33.         System.out.println(response.getStatusLine());  
  34.         HttpEntity entity = response.getEntity();  
  35.         if (null != entity){  
  36.             System.out.println("Response content length: " + entity.getContentLength());  
  37.             System.out.println("Response content:\n" + EntityUtils.toString(entity));  
  38.         }  
  39.     }  
  40.   
  41. }  

发现返回的网页内容 显示美国ip,代理成功。再接再厉。


尝试通过代理注册帐号

[java] view plaincopy
  1. package com.zhh.examples.client;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.apache.http.Consts;  
  7. import org.apache.http.HttpEntity;  
  8. import org.apache.http.HttpHost;  
  9. import org.apache.http.HttpResponse;  
  10. import org.apache.http.NameValuePair;  
  11. import org.apache.http.auth.AuthScope;  
  12. import org.apache.http.auth.UsernamePasswordCredentials;  
  13. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  14. import org.apache.http.client.methods.HttpGet;  
  15. import org.apache.http.client.methods.HttpPost;  
  16. import org.apache.http.conn.params.ConnRoutePNames;  
  17. import org.apache.http.cookie.Cookie;  
  18. import org.apache.http.impl.client.DefaultHttpClient;  
  19. import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;  
  20. import org.apache.http.message.BasicNameValuePair;  
  21. import org.apache.http.util.EntityUtils;  
  22.   
  23. public class RegDG {  
  24.   
  25.     public static void main(String[] args) throws Exception {  
  26.   
  27.         String proxyUrl = "127.0.0.1";  
  28.         int proxyPort = 8087;  
  29.         String url = "http://www.baidu.com/?a=btpcgo";//baidu为虚拟  
  30.   
  31.         DefaultHttpClient client = new DefaultHttpClient(new ThreadSafeClientConnManager());  
  32.         //设置代理  
  33.         client.getCredentialsProvider().setCredentials(  
  34.                                 new AuthScope(proxyUrl, proxyPort),  
  35.                                 new UsernamePasswordCredentials(""""));  
  36.         HttpHost proxy = new HttpHost(proxyUrl, proxyPort);  
  37.         client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);  
  38.           
  39.         //请求url 使a=btpcgo存入cookie 注册时服务端会检测是哪个用户邀请  
  40.         HttpGet get = new HttpGet(url);  
  41.         HttpResponse response = client.execute(get);  
  42.           
  43.         System.out.println(response.getStatusLine());  
  44.         HttpEntity entity = response.getEntity();  
  45.         if (null != entity){  
  46.             System.out.println("Response content length: " + entity.getContentLength());  
  47. //          System.out.println("Response content:\n" + EntityUtils.toString(entity));  
  48.         }  
  49.         //查看cookies值  
  50.         List<Cookie> cookies = client.getCookieStore().getCookies();  
  51.         if (null != cookies){  
  52.             for (Cookie cookie:cookies){  
  53.                 System.out.println(cookie.getName()+" - " + cookie.getValue());  
  54.             }  
  55.         }  
  56.           
  57.         //注册  
  58.         String regUrl = "http://www.baidu.com/register.php";  
  59.         String regName = "test" + (int)(Math.random()*100000);  
  60.         String regPwd = "12345679";  
  61.         String regEmail = regName + "@163.com";  
  62.         System.out.println("=============Register===============" + regName);  
  63.           
  64.           
  65.         HttpPost regPost = new HttpPost(regUrl);  
  66.         List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
  67.         nvps.add(new BasicNameValuePair("forward"""));  
  68.         nvps.add(new BasicNameValuePair("step""2"));  
  69.         nvps.add(new BasicNameValuePair("_hexie""a3fb2f2b"));  
  70.         nvps.add(new BasicNameValuePair("regname", regName));  
  71.         nvps.add(new BasicNameValuePair("regpwd", regPwd));  
  72.         nvps.add(new BasicNameValuePair("regpwdrepeat", regPwd));  
  73.         nvps.add(new BasicNameValuePair("regemail", regEmail));  
  74.         nvps.add(new BasicNameValuePair("rgpermit""1"));  
  75.         regPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));  
  76.         HttpResponse response_reg = client.execute(regPost);  
  77.         HttpEntity regEntity = response_reg.getEntity();  
  78.         System.out.println(response_reg.getStatusLine());  
  79.         if (null != regEntity) {  
  80.             String content = new String(EntityUtils.toString(regEntity).getBytes("iso-8859-1"), "gbk");  
  81.             System.out.println("Response content:\n" + content);  
  82.         }  
  83.           
  84.         client.getConnectionManager().shutdown();  
  85.     }  
  86.   
  87. }  
运行发现注册成功!GAE服务器ip大概一分钟最自动更换,大概是选择自动选择最空闲的服务器,但总共大概只有上百个ip来回用,不方便大量刷。
如果有大量代理服务器,可以依次替换不同代理服务器注册,效率更高。

对于注册有验证码的,可以使用 "Tesseract-ocr" 或 “tesseract” 来自动识别验证码,对一般验证码还是相当有把握的。

0 0
原创粉丝点击