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

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

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

       

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

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

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

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

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

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

104
105        private void BtnLogin_Click(object sender, EventArgs e)
106        
107            Login("davi97""sffg");
108        }

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

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

132
133


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

posted @ 2007-07-13 10:11  mp3 swf  阅读(1824)  评论(9编辑  收藏  举报