简体   繁体   English

HttpWebRequest For Atom URL返回HTML内容

[英]HttpWebRequest For Atom URL returns HTML Content

I'm trying to load an an Atom feed into a string and I'm using the following method: 我正在尝试将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();
        }

This works for most feeds, but http://tipsforbbq.com/RSS , which renders as an atom feed in my browser, returns an HTML page (the one found at http://tipsforbbq.com/ ). 这适用于大多数提要,但是http://tipsforbbq.com/RSS在我的浏览器中显示为原子提要,它返回HTML页面(可在http://tipsforbbq.com/找到)。 Does anyone know why this might be happening? 有谁知道为什么会这样?

I had a similar problem with HttpWebRequest and I resolved it using a well known User Agent. 我在HttpWebRequest上也遇到了类似的问题,并使用了著名的用户代理解决了该问题。

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

I hope this can help you. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM