Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.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# Atom URL的HttpWebRequest返回HTML内容_C#_Rss_Atom Feed - Fatal编程技术网

C# Atom URL的HttpWebRequest返回HTML内容

C# Atom URL的HttpWebRequest返回HTML内容,c#,rss,atom-feed,C#,Rss,Atom Feed,我尝试将Atom提要加载到字符串中,并使用以下方法: private string GetXml(string urlString) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;

我尝试将Atom提要加载到字符串中,并使用以下方法:

private string GetXml(string urlString)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
            StringBuilder sb = new StringBuilder();
            // Create a new HttpWebRequest object.Make sure that 
            // a default proxy is set if you are behind a firewall.
            HttpWebRequest myHttpWebRequest1 =
              (HttpWebRequest)WebRequest.Create(urlString);

            myHttpWebRequest1.KeepAlive = false;
            myHttpWebRequest1.ContentType = "text/xml";
            // Assign the response object of HttpWebRequest to a HttpWebResponse variable.
            HttpWebResponse myHttpWebResponse1 =
              (HttpWebResponse)myHttpWebRequest1.GetResponse();

            Debug.WriteLine("\nThe HTTP request Headers for the first request are: \n{0}", myHttpWebRequest1.Headers);

            Stream streamResponse = myHttpWebResponse1.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            Char[] readBuff = new Char[256];
            int count = streamRead.Read(readBuff, 0, 256);
            Debug.WriteLine("The contents of the response are.......\n");
            while (count > 0)
            {
                String outputData = new String(readBuff, 0, count);
                sb.Append(outputData);
                count = streamRead.Read(readBuff, 0, 256);
            }
            // Close the Stream object.
            streamResponse.Close();
            streamRead.Close();
            return sb.ToString();
        }

这适用于大多数提要,但在我的浏览器中呈现为atom提要时,会返回一个HTML页面(在中找到的页面)。有人知道为什么会发生这种情况吗?

我在HttpWebRequest中遇到了类似的问题,我使用一个知名的用户代理解决了这个问题

myHttpWebRequest1.UserAgent= @"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";

我希望这能对您有所帮助。

我在HttpWebRequest中遇到了类似的问题,我使用一个知名的用户代理解决了这个问题

myHttpWebRequest1.UserAgent= @"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";

我希望这能对您有所帮助。

您是否已绑定使用用户代理,例如“Mozilla/5.0(Windows NT 6.1;WOW64;rv:40.0)Gecko/20100101 Firefox/40.1”?就是这样!回答一下,我会记下来的。谢谢感谢您使用用户代理,例如“Mozilla/5.0(Windows NT 6.1;WOW64;rv:40.0)Gecko/20100101 Firefox/40.1”?就是这样!回答一下,我会记下来的。谢谢感谢七更好,使用您自己的用户代理,而不是伪装,并提供一个
Accept
头来告诉远程服务您期望的内容。更好的是,使用您自己的用户代理,而不是隐藏,并提供一个
Accept
头来告诉远程服务您期望的内容类型。