Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么变量doc总是返回null?_C#_Winforms - Fatal编程技术网

C# 为什么变量doc总是返回null?

C# 为什么变量doc总是返回null?,c#,winforms,C#,Winforms,我正在从网站下载图片。 代码如下: class MyClient : WebClient { public bool HeadOnly { get; set; } protected override WebRequest GetWebRequest(Uri address) { WebRequest req = base.GetWebRequest(address);

我正在从网站下载图片。 代码如下:

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;
            }
        }
然后,方法:

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;
        }
我试图向方法中添加一般的try-catch,但它仍然到达MessageBox而不是catch

无论如何,我尝试下载的图像链接是:

然后我用了一个断点,在这条线上:

if (type.StartsWith(@"text/html"))
它正在跳转到MessageBox。显示

现在我看到这个类型包含:image/jpeg 我想知道这是否是问题所在,因为没有文本/html或其他东西,可能是链接有问题

编辑**

我尝试更改添加以下内容的方法:

if (type.StartsWith(@"text/html")|| type.StartsWith(@"image/jpeg"))
添加了零件图像/jpeg 但是,在另一个类上使用此方法:

private List<string> getLinks(HtmlAgilityPack.HtmlDocument document)
        {

                List<string> mainLinks = new List<string>();
                var linkNodes = document.DocumentNode.SelectNodes("//a[@href]");
                if (linkNodes != null)
                {
                    foreach (HtmlNode link in linkNodes)
                    {
                        var href = link.Attributes["href"].Value;
                        if (href.StartsWith("http://") == true || href.StartsWith("https://") == true || href.StartsWith("www") == true) // filter for http 
                        {
                            mainLinks.Add(href);
                        }
                    }
                }

                return mainLinks;


        }
private List getLinks(htmlalitypack.HtmlDocument文档)
{
List mainLinks=新列表();
var linkNodes=document.DocumentNode.SelectNodes(“//a[@href]”);
if(linkNodes!=null)
{
foreach(链接节点中的HtmlNode链接)
{
var href=link.Attributes[“href”].Value;
如果(href.StartsWith(“http://”)==true | | href.StartsWith(“https://”)==true | | href.StartsWith(“www”)==true)//http过滤器
{
mainLinks.Add(href);
}
}
}
返回主链接;
}
linkNodes始终为空。 当type.StartsWith为image/jpeg时,此链接节点始终为空。 当type.StartsWith为text/html时,linkNodes不为null


如果需要,我可以将我的项目上传到我的skydrive。

您提供的图像链接指向404…这里有一个有效链接:现在我尝试了另一个网站,下载的图像很好,但这次的变量类型包含:text/html;charset=utf-8,所以我想知道为什么这里的类型是text/html;charset=utf-8及其下载图像,在另一个链接中,类型变量包含image/jpeg而不是text/html;charset=utf-8?@DanielVest哪个图像包含文本/html???否。type.StartsWith的值现在是image/jpeg