Sunday, 25 August 2013

Why the variable doc return null all the time?

Why the variable doc return null all the time?

Im downloading images from a website. This is the code:
class MyClient : WebClient
{
public bool HeadOnly { get; set; }
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest req = base.GetWebRequest(address);
if (HeadOnly && req.Method == "GET")
{
req.Method = "HEAD";
}
return req;
}
}
Then the method:
public static HtmlAgilityPack.HtmlDocument getHtmlDocumentWebClient(string
url, bool useProxy, string proxyIp, int proxyPort, string usename, string
password)
{
HtmlAgilityPack.HtmlDocument doc = null;
try
{
doc = null;
using (MyClient clients = new MyClient())
{
clients.HeadOnly = true;
byte[] body = clients.DownloadData(url);
// note should be 0-length
string type = clients.ResponseHeaders["content-type"];
clients.HeadOnly = false;
// check 'tis not binary... we'll use text/, but could
// check for text/html
if (type == null)
{
return null;
}
else
{
if (type.StartsWith(@"text/html"))
{
string text = clients.DownloadString(url);
try
{
doc = new HtmlAgilityPack.HtmlDocument();
WebClient client = new WebClient();
//client.Headers.Add("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.2; .NET CLR 1.0.3705;)");
client.Credentials =
CredentialCache.DefaultCredentials;
client.Proxy = WebRequest.DefaultWebProxy;
if (useProxy)
{
//Proxy
if (!string.IsNullOrEmpty(proxyIp))
{
WebProxy p = new WebProxy(proxyIp,
proxyPort);
if (!string.IsNullOrEmpty(usename))
{
if (password == null)
password = string.Empty;
NetworkCredential nc = new
NetworkCredential(usename,
password);
p.Credentials = nc;
}
}
}
doc.Load(client.OpenRead(url));
}
catch
{
}
}
}
}
if (doc == null)
{
MessageBox.Show("Doc is null " + doc + " The link
that did it was " + url);
}
}
catch
{
}
return doc;
}
I tried to add general try catch to the method but it still getting to the
MessageBox and not to the catch.
Anyway the link of the image im trying to download is:
http://members.tripod.com/~DannyWest/bundy.jpg
Then i used a breakpoin t and on the line:
if (type.StartsWith(@"text/html"))
Its jumping to the MessageBox.Show...
Now i saw that type contain: image/jpeg I wonder if is that the problem
since there is not text/html or something else maybe something with the
link ?

No comments:

Post a Comment