|
本帖最后由 zjz119 于 2013-9-2 12:05 编辑
- [code=csharp]private void button2_Click(object sender, EventArgs e)
- {
- item = new HttpItem
- {
- Cookie = cookie,
- URL = (ServerURL + "/user/personal.ql"),
- Accept = "text/html, application/xhtml+xml, */*",
- UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
- };
- HttpResult result = http.GetHtml(item);
- string html = result.Html;
- //richTextBox1.AppendText(html + "\r\n\r\n");
- html = Compress(html);
- //richTextBox1.AppendText(html + "\r\n\r\n");
- Match HeroShuLiang = Regex.Match(html, "(?<=英雄数量:</td><td class="n">).*?(?=<)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
- //richTextBox1.AppendText(HeroShuLiang + "\r\n\r\n");
- item.URL = (ServerURL + HerolistURL);
- result = http.GetHtml(item);
- html = result.Html;
- //html = Compress(html);
- //截取英雄列表html
- Match txtHerolist = Regex.Match(html, "(?<=英雄列表 -->).*?(?=英雄信息)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
- //richTextBox1.AppendText(txtHerolist + "\r\n\r\n");
- string _txtHerolist = txtHerolist.Value.ToString();
- richTextBox1.AppendText(_txtHerolist + "\r\n\r\n");
- Regex re = new Regex("<a.*?heroId=(.+?)"><img.*?>LV.(.+?)</span.*?h_name.oit_.">(.+?)<", RegexOptions.IgnoreCase | RegexOptions.Singleline);
- MatchCollection mc = re.Matches(_txtHerolist);
- richTextBox1.AppendText(mc + "\r\n\r\n");
- List<string> idlist = new List<string>();//创建list
- List<string> lvlist = new List<string>();
- List<string> namelist = new List<string>();
- foreach (Match ma in mc)
- {
- idlist.Add(ma.Groups[1].Value.ToString());//捕获数据加入list
- lvlist.Add(ma.Groups[2].Value.ToString());
- namelist.Add(ma.Groups[3].Value.ToString());
- }
- HeroInfoTable = new DataTable("HeroInfo");//加入英雄信息datatable
- string str = "";
- HeroInfoTable.Columns.Add("ID", str.GetType());
- richTextBox1.AppendText(str + "\r\n\r\n");
- HeroInfoTable.Columns.Add("等级", str.GetType());
- HeroInfoTable.Columns.Add("名字", str.GetType());
- string[] Chtml = new string[idlist.Count];
- List<string> tmplist = new List<string>();
- for (int i = 0; i < idlist.Count; i++)
- {
- tmplist.Clear();
- tmplist.Add(idlist[i]);
- tmplist.Add(lvlist[i]);
- tmplist.Add(namelist[i]);
- HeroInfoTable.Rows.Add(tmplist.ToArray());
- }
- comboBox1.Items.Clear();
- for (int i = 0; i < HeroInfoTable.Rows.Count; i++)
- {
- string its = HeroInfoTable.Rows[i][2].ToString() + "-" + HeroInfoTable.Rows[i][1].ToString();
- comboBox1.Items.Add(its);
- }
- }
复制代码 [/code]
可以正常执行
新建一个InfoHelper.cs类- public string GetHeroInfo()
- {
- item = new HttpItem
- {
- Cookie = cookie,
- URL = (ServerURL + "/user/personal.ql"),
- Accept = "text/html, application/xhtml+xml, */*",
- UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
- };
- HttpResult result = http.GetHtml(item);
- string html = result.Html;
- //richTextBox1.AppendText(html + "\r\n\r\n");
- html = Compress(html);
- //richTextBox1.AppendText(html + "\r\n\r\n");
- Match HeroShuLiang = Regex.Match(html, "(?<=英雄数量:</td><td class="n">).*?(?=<)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
- //richTextBox1.AppendText(HeroShuLiang + "\r\n\r\n");
- item.URL = (ServerURL + HerolistURL);
- result = http.GetHtml(item);
- html = result.Html;
- //html = Compress(html);
- //截取英雄列表html
- Match txtHerolist = Regex.Match(html, "(?<=英雄列表 -->).*?(?=英雄信息)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
- //richTextBox1.AppendText(txtHerolist + "\r\n\r\n");
- string _txtHerolist = txtHerolist.Value.ToString();
- //richTextBox1.AppendText(_txtHerolist + "\r\n\r\n");
- Regex re = new Regex("<a.*?heroId=(.+?)"><img.*?>LV.(.+?)</span.*?h_name.oit_.">(.+?)<", RegexOptions.IgnoreCase | RegexOptions.Singleline);
- MatchCollection mc = re.Matches(_txtHerolist);
- //richTextBox1.AppendText(mc + "\r\n\r\n");
- List<string> idlist = new List<string>();//创建list
- List<string> lvlist = new List<string>();
- List<string> namelist = new List<string>();
- foreach (Match ma in mc)
- {
- idlist.Add(ma.Groups[1].Value.ToString());//捕获数据加入list
- lvlist.Add(ma.Groups[2].Value.ToString());
- namelist.Add(ma.Groups[3].Value.ToString());
- }
- HeroInfoTable = new DataTable("HeroInfo");//加入英雄信息datatable
- string str = "";
- HeroInfoTable.Columns.Add("ID", str.GetType());
- HeroInfoTable.Columns.Add("等级", str.GetType());
- HeroInfoTable.Columns.Add("名字", str.GetType());
- string[] Chtml = new string[idlist.Count];
- List<string> tmplist = new List<string>();
- for (int i = 0; i < idlist.Count; i++)
- {
- tmplist.Clear();
- tmplist.Add(idlist[i]);
- tmplist.Add(lvlist[i]);
- tmplist.Add(namelist[i]);
- HeroInfoTable.Rows.Add(tmplist.ToArray());
- }
- return "获取英雄信息成功!";
- }
复制代码 Form1改成- private void button2_Click(object sender, EventArgs e)
- {
- InfoHelper _InfoHelper = new InfoHelper();
- comboBox1.Items.Clear();
- for (int i = 0; i < _InfoHelper.HeroInfoTable.Rows.Count; i++)
- {
- string its = _InfoHelper.HeroInfoTable.Rows[i][2].ToString() + "-" + _InfoHelper.HeroInfoTable.Rows[i][1].ToString();
- comboBox1.Items.Add(its);
- }
- }
复制代码 报发生了未经处理的异常
未将对象引用设置到对象实例。
这是哪里出错了?
|
|