I am trying to fetch HTML from a website. I can successfully fetch it using WebClient but only when I am debugging the code. When I ctrl+f5 it just throws an error and shuts up. Why?
Here's code:
string input = " a href=\"/.* .* /a "; //spaces because < and > will be removed soon.
List<string> matchedlinks = new List<string>();
WebClient wc = new WebClient();
wc.BaseAddress = "http://www.sharpq.com";
wc.Headers.Add("user-agent", @"Mozilla/5.0 (Windows NT 6.0; rv:8.0.1)
Gecko/20100101 Firefox/8.0.1");
Uri baseUri = new Uri("http://www.sharpq.com");
wc.Proxy = null;
string data = "error";
Uri nurl = new Uri("http://www.sharpq.com");
try
{
Stream s = wc.OpenRead(baseUri);
StreamReader sr = new StreamReader(s);
StringBuilder sb = new StringBuilder(sr.ReadToEnd());
data = sb.ToString();
Console.WriteLine(data);
}
catch (System.Net.WebException webexp)
{
Console.WriteLine("Error: " + webexp.Message);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
data=data.Replace('>', ' ').Replace('<', ' ');
if (!data.Equals("error"))
{
try
{
Regex regex = new Regex(data, RegexOptions.IgnoreCase);
foreach (Match m in regex.Matches(input))
{
matchedlinks.Add(m.ToString());
}
}
catch (ArgumentException argx)
{
Console.WriteLine("error: " + argx.Message);
}
}
string query = @"Insert into scriptor_w3schools_Data(rawData, linksData) values (@raw, @links)";
SqlCommand cmd = new SqlCommand(query, scriptor_Connection);
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.Add("@raw", System.Data.SqlDbType.Text).Value = data;
cmd.Parameters.Add("@links", System.Data.SqlDbType.Text).Value = "";
foreach(string s in matchedlinks)
cmd.Parameters["@links"].Value += s;
connect();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
disconnect();
cmd.Dispose();
It has happened all the time. If I run it, it is unable to fetch data from site. If I debug it, data is fetched, but now regex throws error saying bad arguments or something.
I am thinking it is may be because of some time limit on WebClient. Is it so? How can I fetch data. I have tried DownloadString and same thing.
Edit 1: While Runnig, first catch (fetching data). While Debugging, second one(regex)
here is the image of error message generated while running it.
regex exception:
parsing " DOCTYPE html html head meta charset="utf-8" / title sharpq home home page welcome sharpq home industrial training project guidance workshops deep dive sessions12/22/2011 7:58:18 AM /title meta http-equiv="Content" content="text/html; charset=utf-8" / meta name="description" content="sharpq.com offers college kids B.Tech and MCA training, workshops, classess, deep dive sessions and project guidance" / meta name="keywords" content="sharpq.com, SharpQ Solutions, sharpq, career cue, sharpq, sharpq solutions, industrial training, coaching, .net training, asp.net training, MCA training, 6 month training, 6 week training, project guidance, .net projects, asp.net workshops, B. Tech. workshops, B.Tech, kurukshetra, institute of computer, computer technology, deep dive sessions, design classes, design, design workshops, photoshop design" / link href="/Content/styles/base.css" rel="stylesheet" type="text/css" / script src="/Scripts/jquery-1.5.1.min.js" type="te..." - [x-y] range in reverse order.
What is this - [x-y] range in reverse order.?