I wrote this downloadContent() function.
Is there some other way to download the content faster?
private string downloadContent()
{
try
{
WebRequest request = WebRequest.Create(testingUrl);
request.Proxy = null;
request.Method = "GET";
response = request.GetResponse();
Stream stream = response.GetResponseStream();
reader = new StreamReader(stream);
string content = reader.ReadToEnd();
return content;
}
catch
{
return error;
}
}
Now this is the two functions im using to extract the text from in between tags after downloading:
private void GetProfileNames(string text)
{
names = new List<string>();
string startTag = "<span class=\"message-profile-name\" ><a href='/profile/";
string endTag = "'>";
int startTagWidth = startTag.Length;
int endTagWidth = endTag.Length;
index = 0;
while (true)
{
index = text.IndexOf(startTag, index);
if (index == -1)
{
break;
}
// else more to do - index now is positioned at first character of startTag
int start = index + startTagWidth;
index = text.IndexOf(endTag, start + 1);
if (index == -1)
{
break;
}
// found the endTag
profileName = text.Substring(start, index - start);
names.Add(profileName);
}
}
And this is the DoWork() event which is not good the way I did it, but I just don't know how to make it better:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string templastItem;
BackgroundWorker worker = sender as BackgroundWorker;
List<string> tempNamesAndTexts = new List<string>();
string tempDownload = downloadContent();
if (tempDownload == error)
{
if (InvokeRequired)
{
BeginInvoke(new Action(() => textBox1.AppendText(DateTime.Now + "===> " + "Error The Web Site Is Not Responding Wait, Trying To Reconnect" + Environment.NewLine)));
Logger.Write("Error The Web Site Was Not Responding");
}
}
else
{
GetProfileNames(tempDownload);
GetTextFromProfile(tempDownload);
for (int i = 0; i < names.Count; i++)
{
tempNamesAndTexts.Add(names[i] + " " + texts[i]);
}
templastItem = tempNamesAndTexts.Last();
if (InvokeRequired)
{
BeginInvoke(new Action(() => tempNamesAndTexts.ForEach(Item => textBox1.AppendText(DateTime.Now + "===> " + Item + Environment.NewLine))));
string[] array = File.ReadAllLines(full_path_log_file_name);
if (array.Length == 2)
{
foreach (string item in tempNamesAndTexts)
{
Logger.Write(item);
}
}
else
{
if (!array.Contains(templastItem))
{
Logger.Write(templastItem);
}
}
}
}
while (true)
{
testingDownload = downloadContent();
if (testingDownload == error)
{
if (InvokeRequired)
{
BeginInvoke(new Action(() => textBox1.AppendText(DateTime.Now + "===> " + "Error The Web Site Is Not Responding Wait, Trying To Reconnect" + Environment.NewLine)));
Logger.Write("Error The Web Site Was Not Responding");
}
Thread.Sleep(1000);
}
else
{
Logger.exist();
namesAndTexts = new List<string>();
if ((worker.CancellationPending == true))
{
e.Cancel = true;
break;
}
else
{
string content = downloadContent();
if (content == error)
{
if (InvokeRequired)
{
BeginInvoke(new Action(() => textBox1.AppendText(DateTime.Now + "===> " + "Error The Web Site Is Not Responding Wait, Trying To Reconnect" + Environment.NewLine)));
Logger.Write("Error The Web Site Was Not Responding");
}
Thread.Sleep(1000);
}
else
{
GetProfileNames(content);
GetTextFromProfile(content);
for (int i = 0; i < names.Count; i++)
{
namesAndTexts.Add(names[i] + " " + texts[i]);
}
if (InvokeRequired)
{
result = tempNamesAndTexts.SequenceEqual(namesAndTexts);
if (result == true)
{
}
else
{
var t = namesAndTexts.Last();
if (textBox1.InvokeRequired)
{
BeginInvoke(new Action(() => textBox1.AppendText(DateTime.Now + "===> " + t + Environment.NewLine)), null);
Logger.Write(t);
if (result == false)
{
tempNamesAndTexts = new List<string>();
for (int i = 0; i < names.Count; i++)
{
tempNamesAndTexts.Add(names[i] + " " + texts[i]);
}
}
}
}
}
reader.Close();
response.Close();
Thread.Sleep(1000);
}
}
}
}
}
private void GetTextFromProfile(string text)
{
texts = new List<string>();
string str = "<span class=\"message-text\">";
string startTag = str;
string endTag = "</span>";
int startTagWidth = startTag.Length;
int endTagWidth = endTag.Length;
index = 0;
while (true)
{
index = text.IndexOf(startTag, index);
if (index == -1)
{
break;
}
// else more to do - index now is positioned at first character of startTag
int start = index + startTagWidth;
index = text.IndexOf(endTag, start + 1);
if (index == -1)
{
break;
}
// found the endTag
profileNameText = text.Substring(start, index - start);
Conditions();
}
}
And this is the link to view/download the project, which is not big:
https://skydrive.live.com/redir?resid=3B8A7D9F66FF985B!171&authkey=!AFO6EmoF38MtkKQ