原子提要网址上的WebRequest和Stream Reading失败

时间:2012-02-21 04:42:34

标签: c# c#-4.0 c#-3.0 c#-2.0

    Stream FeedReader(string url_)
    {
        var url = url_;

        var USER = "user";
        var PASS = "password";


        var encoded = TextToBase64(USER + ":" + PASS);

        WebRequest myWebRequest = WebRequest.Create(url);
        myWebRequest.Method = "POST";
        myWebRequest.ContentType = "application/x-www-form-urlencoded";
        myWebRequest.PreAuthenticate = true;
        NetworkCredential netCred = new NetworkCredential(USER, PASS);
        myWebRequest.Credentials = netCred;            

        //WebResponse response = myWebRequest.GetResponse();
        var stream = myWebRequest.GetRequestStream();
        return stream;
    }

我使用上面的源代码来获取原子提要流,然后在以下函数中传递

    public static List<Item> ParseAtom2(Stream stream)
    {
        XDocument doc = XDocument.Load(stream);//("atom.xml");
        XNamespace ns = "http://www.w3.org/2005/Atom";

        var entries = doc.Root
             .Elements(ns + "entry")
             .Select(item => new Item
             {
                 FeedType = FeedType.Atom,
                 Content = (string)item.Element(ns + "content"),
                 Link = (string)item.Element(ns + "link").Attribute("href"),
                 PublishDate = (DateTime)item.Element(ns + "published"),
                 Title = (string)item.Element(ns + "title")
             });
        return entries.ToList();
    }

但是我收到一条错误,指出流不可读,我想知道可能是什么问题。 原子Feed网址看起来像http://server.com/posts.atom.xml/

1 个答案:

答案 0 :(得分:0)

您返回RequestStream,它包含将发送到服务器的数据,因此您无法从中读取任何内容,请尝试使用myWebRequest.GetResponse().GetResponseStream()