Witam serdecznie!
W grudniu napisałem sobie z pomocą Google kod, który ze strony:
http://www.tibia.com/community/?subtopic=worlds&world=Menera
Pobiera nicki, levele, oraz profesje i najpierw wrzuca je do listboxa a następnie zapisuje je do pliku txt.
Gdy pisałem ten kod, to wszystko działało ok. Dziś kolega poprosił mnie abym dał mu ten program, ponieważ chce sobie spisać nicki wszystkich postaci online. To ok, odpalam projekt, kompiluje, a następnie uruchamiam. I Co się okazuje? Program przestał dodawać nicki do listboxa. Po debugowaniu programu nie ma żadnych błędów, po prostu po kliknięciu w button nic się nie dzieje. Możliwe ze coś się usunęło w kodzie, albo parsowanie jest źle wykonane, gdyż robiłem to tak bardziej na pamięć.
Oto kod mojej Class'y:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Web;
using System.IO;
namespace ClassLibrary1
{
public partial class Website
{
public delegate void WhoIsOnlineReceived(List<CharOnline> chars);
public static string GetHTML(IAsyncResult ar)
{
HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);
Stream respStream = response.GetResponseStream();
string respBody = string.Empty;
byte[] buf = new byte[8192];
int count = 0;
do
{
count = respStream.Read(buf, 0, buf.Length);
if (count != 0)
respBody += Encoding.ASCII.GetString(buf, 0, count);
}
while (count > 0);
return respBody;
}
public static string Prepare(string text)
{
return System.Web.HttpUtility.HtmlDecode(text) // Decode html character entities
.Replace((char)0xA0, ' '); // Replace non-breaking spaces
}
public static void WhoIsOnline(string worldName, WhoIsOnlineReceived callback)
{
string url = "http://www.tibia.com/community/?subtopic=worlds&world=" + worldName;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.BeginGetResponse(delegate(IAsyncResult ar)
{
string html = GetHTML(ar);
MatchCollection matches = Regex.Matches(html, @"<TD WIDTH=70%><[^<]*>([^<]*)</A></TD><TD WIDTH=10%>([^<]*)</TD><TD WIDTH=20%>([^<]*)</TD></TR>");
List<CharOnline> chars = new List<CharOnline>(matches.Count);
CharOnline co;
for (int i = 0; i < matches.Count; i++)
{
co = new CharOnline();
co.Name = Prepare(matches[i].Groups[1].Value);
co.Level = int.Parse(matches[i].Groups[2].Value);
co.Vocation = Prepare(matches[i].Groups[3].Value);
chars.Add(co);
}
callback(chars);
}, request);
}
public class CharOnline
{
public string Name;
public int Level;
public string Vocation;
}
}
}
A to, główny kod programu:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Boolean isSelected(string searchString)
{
for (int i = 0; i < this.listBox2.SelectedItems.Count; i++)
{
if (listBox2.SelectedItems[i].ToString() == searchString)
{
return true;
}
}
return false;
}
private void button1_Click(object sender, EventArgs e)
{
ClassLibrary1.Website.WhoIsOnline(comboBox1.Text, delegate(List<ClassLibrary1.Website.CharOnline> i)
{
listBox1.Invoke(new EventHandler(delegate
{
for (int z = 0; z < i.Count(); z++)
{
if (comboBox2.Text == "Większy niż:")
{
if (i[z].Level > Int32.Parse(textBox2.Text) && isSelected(i[z].Vocation))
{
listBox1.Items.Add(i[z].Nick);
}
}
}
richTextBox1.Text = richTextBox1.Text + "[" + DateTime.Now.ToString() + "] Znaleziono " + listBox1.Items.Count + " Graczy. Obecnie jest " + i.Count() + " Graczy online.\r\n";
}));
});
}
}
}
ComboBox2 zawiera w sobie tekst: 'Większy niż:'
textBox2 zawiera w moim przypadku liczbę '1'
listBox2.SelectedItems wybrałem świat Menera.
A tak wygląda kod strony, którą chce sparsować:
<td style="width:70%;text-align:left;">
<a name="A"/>
<a href="http://www.tibia.com/community/?subtopic=characters&name=Aanjo">Aanjo</a>
</td>
<td style="width:10%;">170</td>
<td style="width:20%;">Elder Druid</td>
</tr>
Oczywiście te dane jak: Aanjo, 170, Elder Druid musi pobierać osobno, dla każdej z tabelek.
I nie mam pojęcia, czemu po kliknięciu w button, nie chce pobierać tych nicków ;/
Proszę o pomoc ;)