使用java编程,实现论坛自动登录、签到、领奖、摇一摇

公司论坛有一个活动,每天在灌水区回帖1次,可以领到一张换礼卡;如果回帖3次,可以得到一次摇一摇的机会,随机摇出一定数量的贝壳,贝壳又可以换成换礼卡。由于是公司信息部门的业务员,所以可以拥有一台测试服务器的操作权限。

二话不说,先上代码:

package com.ymc;

import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class MyOwnPro{
	private static URL url;  
    private static HttpURLConnection con;  
    private static String temp;  
    private static InputStream is;  
    private static byte[] b;  
    private static int forhash_pos = -1;  
    private static OutputStreamWriter osw;
    private static String cookie_sid;  
    private static String cookie_auth;  
    private static String cookie_saltkey;
    private static String my_cookie;  
    private static String replay_formhash;
    private static String roll_formhash;
    private static String username = "yanmaochang";// 用户名  
    private static String password = "此处填写MD5密码";// yanmaochang的MD5加密后的密码
    private static String msg = "子曰:每日签仨到,不亦乐乎?";// 帖子内容  
    
    
    
    public static void getCookieSid(){
    	try { 
	    	new MyLog("进入获取cookid和login的formhash的阶段");
	        url = new URL("http://bbs.地址保密.com:88/portal.php");  
	        con = (HttpURLConnection) url.openConnection();  
	        con.setRequestProperty("User-Agent", "Internet Explorer");
	        if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {  
	        	new MyLog("获取服务器发给客户端的 Cookie");
	            temp = con.getHeaderField("Set-Cookie");  
	            new MyLog("Set-Cookie:" + temp);  
	            // 取 Cookie 前面的部分就可以了,后面是过期时间、路径等,不用管它  
	            cookie_sid = temp.substring(0, 20);  
	            new MyLog("提取客户端的 Cookie");
	            new MyLog("cookie_sid = "+cookie_sid);  
	        }
    	 } catch (Exception e) {  
             e.printStackTrace();  
         }  
    }
    
    public static void getCookieAuth(){
    	try {
    		new MyLog("获取cookie_auth等内容");  
            url = new URL("http://bbs.地址保密.com:88/member.php?mod=logging");  
            con = (HttpURLConnection) url.openConnection();  
            con.setRequestProperty("User-Agent", "Internet Explorer");
            // 设定以 POST 发送  
            con.setRequestMethod("POST");  
            // 加入 Cookie 内容  
            con.setRequestProperty("Cookie", cookie_sid);
            // 添加 POST 的内容  
            con.setDoOutput(true);  
            osw = new OutputStreamWriter(con.getOutputStream());  
            osw.write("action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1&fastloginfield=username"
            		+"&username="+username
            		+"&password="+password
            		+"&quickforward=yes&handlekey=ls");  
            
            osw.flush();  
            osw.close();  
            
            if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {  
                Map> map = con.getHeaderFields();  
                List list = map.get("Set-Cookie");  
                new MyLog("list = "+list+", list.size() = "+list.size());
                for (int i = 0; i < list.size(); i++) {  
                    temp = list.get(i);  
                    if (temp.contains("_auth")) {  
                        cookie_auth = temp.split(";")[0];  
                        new MyLog("cookie_auth ===================" + cookie_auth); 
                    }
                    if(temp.contains("_saltkey")){
                    	cookie_saltkey = temp.split(";")[0];  
                        new MyLog("cookie_saltkey ===================" + cookie_saltkey); 
                    }
                }  
            }  
            
            my_cookie = cookie_sid + ";" + cookie_auth + ";" + cookie_saltkey;  
            new MyLog("制作Cookie得到: "+my_cookie);
		} catch (Exception e) {
			 e.printStackTrace();  
		}
    }
    
    public static void applyTask(){
    	try {
	          new MyLog("领取灌水任务");
	          url = new URL("http://bbs.地址保密.com:88/home.php?mod=task");
	          con = (HttpURLConnection) url.openConnection();  
	          con.setRequestProperty("User-Agent", "Internet Explorer");
	          new MyLog("POST 发送 "); 
	          con.setRequestMethod("POST"); 
	          new MyLog(" Cookie 内容  ");
	          con.setRequestProperty("Cookie", my_cookie); 
	          new MyLog(" POST 的内容 "); 
	          con.setDoOutput(true); 
	          osw = new OutputStreamWriter(con.getOutputStream());  
	          osw.write("do=apply&id=5");  
	          osw.flush();  
	          osw.close(); 
	          if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {  
	          		new MyLog("领取灌水成功");
	          }
    	} catch (Exception e) {
			 e.printStackTrace();  
		}
    }
    
    
    public static void getReplyFormhash(){
    	try {
    		 new MyLog("找到发帖formhash ");
    		 url = new URL("http://bbs.地址保密.com:88/forum.php?mobile=no&mod=post&action=reply&fid=57&tid=1012");  
    		 con = (HttpURLConnection) url.openConnection();  
    		 con.setRequestProperty("User-Agent", "Internet Explorer");
    		 con.setRequestMethod("POST");  
    		 con.setRequestProperty("Cookie", my_cookie);  
    		 con.setRequestProperty("Accept", "text/html, application/xhtml+xml, */*"); 
    		 con.setRequestProperty("Content-type", "text/html");
    		 con.setRequestProperty("Cookie", my_cookie); 
    		 con.setDoOutput(true);  
          
    		 osw = new OutputStreamWriter(con.getOutputStream());  
    		 osw.flush();  
    		 osw.close();  
 
    		 if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {  
    			 new MyLog("获取客户端接收到的页面代码");
    			 is = con.getInputStream();  
    			 b = new byte[100000];  
    			 is.read(b);
    			 temp = new String(b); 
    			 new MyLog("找到页面代码中formhash的位置");
    			 forhash_pos = temp.indexOf("name=\"formhash"); 
    			 new MyLog("formhash的位置 = "+forhash_pos);
    			 new MyLog("找出这个 formhash 的内容,这是回复用的 formhash");
    			 replay_formhash = temp.substring(forhash_pos + 23, forhash_pos + 23 + 8);  
    			 new MyLog("replay_formhash = " + replay_formhash);  
    			 is.close();   
    		 }  
		} catch (Exception e) {
			e.printStackTrace();  
		}
    }
    
    
    public static void replyMessage(){
    	try {
    		new MyLog("进行一次回帖");
          url = new URL("http://bbs.地址保密.com:88/forum.php?mod=post");  
          con = (HttpURLConnection) url.openConnection();  
          con.setRequestProperty("User-Agent", "Internet Explorer");
          con.setRequestMethod("POST");  
          con.setRequestProperty("Cookie", my_cookie);  
          con.setDoOutput(true);  
          osw = new OutputStreamWriter(con.getOutputStream(),"GBK");  
          osw.write("&action=reply&fid=57&tid=1012&extra=&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1"
        		+"&message=" + msg
          		+"&posttime=" + (new Date()).getTime()/1000
          		+"&formhash=" + replay_formhash
          		+"&usesig=1&subject=++");  
        
          osw.flush();  
          osw.close();
          
          if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {  
        	  new MyLog("貌似回复成功"); 
          } 
		} catch (Exception e) {
			e.printStackTrace(); 
		}
    }
    
    
    public static void drawTask(){
    	try {
	          new MyLog("领取灌水奖励");
	          url = new URL("http://bbs.地址保密.com:88/home.php?mod=task");
	          con = (HttpURLConnection) url.openConnection();  
	          con.setRequestProperty("User-Agent", "Internet Explorer");
	          new MyLog("POST 发送 "); 
	          con.setRequestMethod("POST"); 
	          new MyLog(" Cookie 内容  ");
	          con.setRequestProperty("Cookie", my_cookie); 
	          new MyLog(" POST 的内容 "); 
	          con.setDoOutput(true); 
	          osw = new OutputStreamWriter(con.getOutputStream());  
	          osw.write("do=draw&id=5");  
	          osw.flush();  
	          osw.close(); 
	          if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {  
	          		new MyLog("领取奖励成功");
	          }
    	} catch (Exception e) {
			 e.printStackTrace();  
		}
    }
    
    
    
    public static void getRollFormhash(){
    	try {
    		 new MyLog("找到发帖formhash ");
    		 url = new URL("http://bbs.地址保密.com:88/plugin.php?id=yinxingfei_zzza:yinxingfei_zzza_rank");  
    		 con = (HttpURLConnection) url.openConnection();  
    		 con.setRequestProperty("User-Agent", "Internet Explorer");
    		 con.setRequestMethod("POST");  
    		 con.setRequestProperty("Cookie", my_cookie);  
    		 con.setRequestProperty("Accept", "text/html, application/xhtml+xml, */*"); 
    		 con.setRequestProperty("Content-type", "text/html");
    		 con.setRequestProperty("Cookie", my_cookie); 
    		 con.setDoOutput(true);  
          
    		 osw = new OutputStreamWriter(con.getOutputStream());  
    		 osw.flush();  
    		 osw.close();  
 
    		 if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {  
    			 new MyLog("获取客户端接收到的页面代码");
    			 is = con.getInputStream();  
    			 b = new byte[100000];  
    			 is.read(b);
    			 temp = new String(b); 
    			 new MyLog("找到页面代码中formhash的位置");
    			 forhash_pos = temp.indexOf("name=\"formhash"); 
    			 new MyLog("formhash的位置 = "+forhash_pos);
    			 new MyLog("找出这个 formhash 的内容,这是回复用的 formhash");
    			 roll_formhash = temp.substring(forhash_pos + 23, forhash_pos + 23 + 8);  
    			 new MyLog("roll_formhash = " + roll_formhash);  
    			 is.close();   
    		 }  
		} catch (Exception e) {
			e.printStackTrace();  
		}
    }
    
    
    public static void goRoll(){
    	try {
    		new MyLog("去摇摇乐");
    		url = new URL("http://bbs.地址保密.com:88/plugin.php?id=yinxingfei_zzza:yinxingfei_zzza_post");  
            con = (HttpURLConnection) url.openConnection();  
            con.setRequestProperty("User-Agent", "Internet Explorer");
            con.setRequestMethod("POST");  
            con.setRequestProperty("Cookie", my_cookie);  
            con.setDoOutput(true);  
            osw = new OutputStreamWriter(con.getOutputStream(),"GBK");  
            osw.write("formhash="+replay_formhash);
            osw.flush();  
            osw.close();
            
            if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {  
          	  new MyLog("貌似摇一摇成功"); 
                //输出得到页面的内容
                is = con.getInputStream();  
                byte[] b = new byte[1000000];  
                is.read(b);  
                new MyLog(new String(b));  
                is.close();  
            }
		} catch (Exception e) {
			e.printStackTrace(); 
		}
    }
	public static void main(String[] args) {

		try {  
			getCookieSid();
            System.out.println("----------------------------------------------------------------------------"); 
            getCookieAuth();
            System.out.println("----------------------------------------------------------------------------"); 
            applyTask();
            System.out.println("----------------------------------------------------------------------------");
            while(forhash_pos == -1){
            	getReplyFormhash();
            }
            System.out.println("----------------------------------------------------------------------------");
            for(int i=1; i<=3; i++){
            	replyMessage();
            }
            System.out.println("----------------------------------------------------------------------------");
            drawTask();
            System.out.println("----------------------------------------------------------------------------");
            while(forhash_pos == -1){
            	getRollFormhash();
            }
            System.out.println("----------------------------------------------------------------------------");
           	Thread.sleep(5000);//等待5秒
            goRoll();
          
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
	}
}

其中Mylog是我自己写的打印方法,可忽视。

MD5密码获取方法:

使用“网刃”网络数据拦截工具,截取正常登录过程中电脑发送出去的数据,里面就有经过MD5加密后的密码。

服务器部署方法:

将工程打包成可运行的jar文件(MyAutoSignYMC.jar),远程登录服务器(此服务器并非论坛所在服务器)桌面,编写bat运行文件,内容为一句话:
start java -jar MyAutoSignYMC.jar
然后通过服务器控制面板添加任务计划,每天运行bat文件一次即可。

你可能感兴趣的:(网络,java)