VS2005写的一个在线游戏Travian的机器人小工具雏形

最近,一个朋友推荐了一个网页版的在线游戏,休息时可以玩玩调节一下心情,游戏地址是:http://s1.travian.china.com/login.php

玩了玩,感觉还挺有意思,不过老是需要去看看网页进度比较累,所以就想着写个工具来自动化操作(没办法,RD一般都喜欢偷懒的),之前还没有用WinForm操作过Cookie,为了保持Cookie,中间还出现了一点小小挫折,不过现在已经搞定了,把Cookie保持的代码贴给大家看看,有兴趣做类似工具的人可以参考一下。

       

  1 None.gif // Cookies集合保存
  2 None.gif          public  CookieCollection CCol  =   null ;
  3 None.gif
  4 None.gif         // 设置公司代理
  5 None.gif          public  WebProxy GetWP()
  6 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
  7InBlock.gif            WebProxy _WP = new WebProxy("h00proxy"80);
  8InBlock.gif            _WP.BypassProxyOnLocal = true;
  9InBlock.gif            NetworkCredential _CD = new NetworkCredential("davi xiong""asdfad""bqc");
 10InBlock.gif            _WP.Credentials = _CD;
 11InBlock.gif            return _WP;
 12ExpandedBlockEnd.gif        }

 13 None.gif
 14 None.gif         private   void  Login( string  strId,  string  strPassword)
 15 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 16InBlock.gif
 17InBlock.gif            ASCIIEncoding encodingA = new ASCIIEncoding();
 18InBlock.gif
 19InBlock.gif            CookieContainer myCookieContainer = new CookieContainer();
 20InBlock.gif
 21InBlock.gif            progressBar1.Value = 0;  // Process
 22InBlock.gif            //=======GET================================================================================
 23InBlock.gif            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://s1.travian.china.com/login.php");
 24InBlock.gif            myRequest.Proxy = GetWP();
 25InBlock.gif
 26InBlock.gif            myRequest.CookieContainer = myCookieContainer;
 27InBlock.gif
 28InBlock.gif            progressBar1.Value = 10;  // Process
 29InBlock.gif            HttpWebResponse myResponseA = (HttpWebResponse)myRequest.GetResponse();
 30InBlock.gif            StreamReader readerA = new StreamReader(myResponseA.GetResponseStream(), Encoding.Default);
 31InBlock.gif            string Getcontent = readerA.ReadToEnd();
 32InBlock.gif
 33InBlock.gif            progressBar1.Value = 20;  // Process
 34InBlock.gif            Regex Reg = new Regex("<input type=\"hidden\" name=\"login\" value=\"(.*)\">");
 35InBlock.gif            string login = "";
 36InBlock.gif            if (Reg.IsMatch(Getcontent))
 37ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 38InBlock.gif                Match Mc = Reg.Matches(Getcontent)[0];
 39InBlock.gif                login = Mc.Groups[1].Value;
 40ExpandedSubBlockEnd.gif            }

 41InBlock.gif            progressBar1.Value = 30;  // Process
 42InBlock.gif
 43InBlock.gif            Reg = new Regex("<input class=\"fm fm110\" type=\"text\" name=\"(.*)\" value=");
 44InBlock.gif            string name = "";
 45InBlock.gif            if (Reg.IsMatch(Getcontent))
 46ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 47InBlock.gif                Match Mc = Reg.Matches(Getcontent)[0];
 48InBlock.gif                name = Mc.Groups[1].Value;
 49ExpandedSubBlockEnd.gif            }

 50InBlock.gif
 51InBlock.gif            progressBar1.Value = 40;  // Process
 52InBlock.gif            Reg = new Regex("<input class=\"fm fm110\" type=\"password\" name=\"(.*)\" value=");
 53InBlock.gif            string pass = "";
 54InBlock.gif            if (Reg.IsMatch(Getcontent))
 55ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 56InBlock.gif                Match Mc = Reg.Matches(Getcontent)[0];
 57InBlock.gif                pass = Mc.Groups[1].Value;
 58ExpandedSubBlockEnd.gif            }

 59InBlock.gif
 60InBlock.gif            progressBar1.Value = 50;  // Process
 61InBlock.gif            Reg = new Regex("<p align=\"center\"><input type=\"hidden\" name=\"(.*)\" value=\"(.*)\">");
 62InBlock.gif            string hid2name = "";
 63InBlock.gif            string hid2value = "";
 64InBlock.gif            if (Reg.IsMatch(Getcontent))
 65ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 66InBlock.gif                Match Mc = Reg.Matches(Getcontent)[0];
 67InBlock.gif                hid2name = Mc.Groups[1].Value;
 68InBlock.gif                hid2value = Mc.Groups[2].Value;
 69ExpandedSubBlockEnd.gif            }

 70InBlock.gif
 71InBlock.gif            //=======DATA==========================================================
 72InBlock.gif            progressBar1.Value = 60;  // Process
 73InBlock.gif            ASCIIEncoding encoding = new ASCIIEncoding();
 74InBlock.gif            string postData = name + "=" + strId;
 75InBlock.gif            postData += "&" + pass + "=" + strPassword;
 76InBlock.gif            postData += "&login=" + login;
 77InBlock.gif            postData += "&autologin=ja&" + hid2name + "=" + hid2value + "&w=1024:768";
 78InBlock.gif
 79InBlock.gif            byte[] data = encoding.GetBytes(postData);
 80InBlock.gif
 81InBlock.gif            //=======POST================================================================================
 82InBlock.gif            progressBar1.Value = 70;  // Process
 83InBlock.gif            myRequest = (HttpWebRequest)WebRequest.Create("http://s1.travian.china.com/dorf1.php");
 84InBlock.gif            myRequest.Proxy = GetWP();
 85InBlock.gif
 86InBlock.gif            myRequest.Method = "POST";
 87InBlock.gif            myRequest.ContentType = "application/x-www-form-urlencoded";
 88InBlock.gif            myRequest.ContentLength = data.Length;
 89InBlock.gif            myRequest.CookieContainer = myCookieContainer; 
 90InBlock.gif            Stream newStream = myRequest.GetRequestStream();
 91InBlock.gif            newStream.Write(data, 0, data.Length);
 92InBlock.gif            newStream.Close();
 93InBlock.gif
 94InBlock.gif            progressBar1.Value = 80;  // Process
 95InBlock.gif            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
 96InBlock.gif            CCol = myCookieContainer.GetCookies(myRequest.RequestUri);
 97InBlock.gif            progressBar1.Value = 90;  // Process
 98InBlock.gif            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
 99InBlock.gif            string content = reader.ReadToEnd();
100InBlock.gif
101InBlock.gif            progressBar1.Value = 100;  // Process
102InBlock.gif            progressBar1.Value = 0;  // Process
103ExpandedBlockEnd.gif        }

104 None.gif
105 None.gif         private   void  BtnLogin_Click( object  sender, EventArgs e)
106 ExpandedBlockStart.gifContractedBlock.gif         dot.gif
107InBlock.gif            Login("davi97""sffg");
108ExpandedBlockEnd.gif        }

109 None.gif
110 None.gif         // 获取登录后才能查看的网页数据
111 None.gif          private   string  GetPageData( string  URL)
112 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
113InBlock.gif            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
114InBlock.gif            myRequest.Proxy = GetWP();
115InBlock.gif
116InBlock.gif            myRequest.CookieContainer = new CookieContainer();
117InBlock.gif            myRequest.CookieContainer.Add(new Uri(URL), CCol);
118InBlock.gif
119InBlock.gif            HttpWebResponse myResponseA = (HttpWebResponse)myRequest.GetResponse();
120InBlock.gif            StreamReader readerA = new StreamReader(myResponseA.GetResponseStream(), Encoding.Default);
121InBlock.gif            return readerA.ReadToEnd();
122ExpandedBlockEnd.gif        }

123 None.gif
124 None.gif         private   void  BtnSearch_Click( object  sender, EventArgs e)
125 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
126InBlock.gif            string URL = "http://s1.travian.china.com/karte.php";
127InBlock.gif            string ReqContent = GetPageData(URL);
128InBlock.gif
129InBlock.gif            //TODO : 自动搜索地图上的人员信息
130InBlock.gif  
131ExpandedBlockEnd.gif }

132 None.gif
133 None.gif


平时无聊的朋友也可以玩玩,还是蛮有意思的,也可以找我,我的帐号是davi97

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值