Hi, i'm trying to get a remote file and then do some regular expressions on the content. So far i have the following:
The GetContent function simply gets the file from the url passed in:
The problem i have is that the application hangs after displaying Check 1 and the matching results line never gets displayed. From this i assume the contents of the file is retrieved successfully but the regular expression is slow. Appreciate if someone could tell me how i can modify my regular expression to make it more efficent or how i could add a timeout of the regular expression.
Cheers. Lee
Code:
lstOutput.Items.Add("Checking http://thecaptainsdeck.net/index.php?action=arcade;sa=hightscore;game=1");
string pageContent = this.GetContent("http://thecaptainsdeck.net/index.php?action=arcade;sa=hightscore;game=1");
lstOutput.Items.Add("Check 1");
Match match = Regex.Match(pageContent, "<a(.*?)href=\"(.*?)\"(.*?)>(.*?)(Contact|Link|Submit Site|Add Site|Submit Your Site|Add Your Site)(.*?)</a>", RegexOptions.IgnoreCase);
lstOutput.Items.Add("Matching Results: " + match.Index.ToString());
The GetContent function simply gets the file from the url passed in:
Code:
private string GetContent(string url)
{
string result = "Error communicating with server";
// Check if the page has already been checked
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(url);
wreq.Method = "GET";
wreq.Timeout = 5000; // 5 seconds or 5,000 milliseconds
try
{
HttpWebResponse wr = (HttpWebResponse)wreq.GetResponse();
if (wr.StatusCode == HttpStatusCode.OK)
{
StreamReader sr = new StreamReader(wr.GetResponseStream());
result = sr.ReadToEnd();
sr.Close();
}
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.Timeout)
{
result = "The request has timed out!";
}
else
{
result = "There was some exception: " + ex.Message;
}
}
return result;
}
The problem i have is that the application hangs after displaying Check 1 and the matching results line never gets displayed. From this i assume the contents of the file is retrieved successfully but the regular expression is slow. Appreciate if someone could tell me how i can modify my regular expression to make it more efficent or how i could add a timeout of the regular expression.
Cheers. Lee