WebRequest 和 Stream Reading 在 Atom feed url 上失败
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您返回 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()