Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.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# 使用chrome发送HttpWebRequest_C#_Google Chrome_Web Scraping_Httpwebrequest - Fatal编程技术网

C# 使用chrome发送HttpWebRequest

C# 使用chrome发送HttpWebRequest,c#,google-chrome,web-scraping,httpwebrequest,C#,Google Chrome,Web Scraping,Httpwebrequest,我的网页抓取代码是 public static string getSourceCode(string url) { try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.UserAgent = @"Mozilla/5.0 (Windows NT 6.2; Win64; x

我的网页抓取代码是

public static string getSourceCode(string url)
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.UserAgent = @"Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36";
                //req.UserAgent = "Mozilla/5.0";
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader sr = new StreamReader(res.GetResponseStream());
                string sourceCode = sr.ReadToEnd();
                sr.Close();
                res.Close();
                return sourceCode;
            }catch(Exception ae){
            return string.Empty;
            }

        }
我只想使用chrome发送此请求,因为源代码因浏览器而异


有什么解决方案吗?

与其他浏览器相比,chrome的源代码到底有什么不同?我能看到web服务器根据发送请求的浏览器更改源代码的唯一方法是通过您已经在使用的用户代理。chrome完全有可能在内部执行与其他浏览器不同的源代码格式设置。在不了解您的具体情况的情况下,我唯一能真正建议的是使用web调试代理工具,例如查看chrome发送到相关url的头,然后尽最大努力在代码中模拟头。如果这没有帮助,那么chrome显然是在内部更改代码。根据您注意到的差异,在从服务器获得响应后,您很可能需要手动操作源代码,以匹配您所需的源代码。@JoeyJoejrShabado很抱歉回复太晚,thnx很抱歉您的回复,事实上,我的网站上有一些部分,所以根据这些部分html数据的变化,浏览器中的url是相同的。我刚刚给了chrome作为我的选择,当这个部分在chrome html上更改时,我也收到了更改。