System.Net.WebResponse response = null;try{ // Setup our Web request System.Net.WebRequest request = System.Net.WebRequest.Create(pageUrl); request.Timeout = timeoutSeconds * 1000; // Retrieve data from request response = request.GetResponse(); System.IO.Stream streamReceive = response.GetResponseStream(); System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("utf-8"); System.IO.StreamReader streamRead = new System.IO.StreamReader( streamReceive, encoding); // return the retrieved HTML return streamRead.ReadToEnd(); }catch (Exception ex){ // Error occured grabbing data, return empty string. MessageBox.Show("Error"); return "";}finally{ // Check if exists, then close the response. if ( response != null ) { response.Close(); }}
Regex regex = new Regex("href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))", RegexOptions.IgnoreCase|RegexOptions.Compiled );for ( Match match = regex.Match( html ); match.Success; match = match.NextMatch() ) { MessageBox.Show( match.Groups[1].ToString() );}