WebRequest 和 Stream Reading 在 Atom feed url 上失败

发布于 2025-01-07 16:56:16 字数 1599 浏览 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();
    }

但是我收到一条错误,指出该流不可读,我想知道可能是什么问题。 Atom feed url 看起来像这样 http://server.com/posts.atom.xml/

    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;
    }

I am using the above source code to get the atom feed stream which is then passed in the following function

    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();
    }

I however get an error stating that the stream in unreadable, I would like to know what might have been the problem.
The atom feed url looks like this http://server.com/posts.atom.xml/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咆哮 2025-01-14 16:56:16

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

You return RequestStream it contains data that would be sent to server so you can't read anything from it, try using myWebRequest.GetResponse().GetResponseStream()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文