C# - 如何通过 HTTP 读取连续的 XML 流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/775574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# - How to read a continuous stream of XML over HTTP
提问by
I am trying to figure out the best way to consume a continuous stream of XML data from a service that is sending the data as a "constant" feed over HTTP.
我试图找出从服务中使用连续 XML 数据流的最佳方法,该服务通过 HTTP 将数据作为“恒定”提要发送。
I was considering using HttpWebRequest/Response but I am not sure how that will behave if the data just continuously streams.
我正在考虑使用 HttpWebRequest/Response,但我不确定如果数据只是连续流,它会如何表现。
Any thoughts?
有什么想法吗?
回答by esac
I have done this before, not with XML, but with data that needed to be parsed for state changes for an application. HttpWebResponse.GetResponseStream() method worked fine for this. Make sure to call Close() on this stream when you are done. I suggest a finally block.
我以前这样做过,不是使用 XML,而是使用需要解析应用程序状态更改的数据。HttpWebResponse.GetResponseStream() 方法为此工作得很好。完成后,请确保在此流上调用 Close()。我建议使用 finally 块。
HttpWebRequest req;
try
{
req = (HttpWebRequest)WebRequest.Create("http://www.example.com");
Stream stream = req.GetResponseStream();
byte[] data = new byte[4096];
int read;
while ((read = data.Read(data, 0, data.Length)) > 0)
{
Process(data, read);
}
}
finally
{
if (req != null)
req.Close();
}
Or, alternatively:
或者,或者:
HttpWebRequest req;
try
{
req = (HttpWebRequest)WebRequest.Create("http://www.example.com");
Stream stream = req.GetResponseStream();
XmlTextReader reader = new XmlTextReader(stream);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
Console.Write("<{0}>", reader.Name);
break;
case XmlNodeType.Text:
Console.Write(reader.Value);
break;
case XmlNodeType.CDATA:
Console.Write("<![CDATA[{0}]]>", reader.Value);
break;
case XmlNodeType.ProcessingInstruction:
Console.Write("<?{0} {1}?>", reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
Console.Write("<!--{0}-->", reader.Value);
break;
case XmlNodeType.XmlDeclaration:
Console.Write("<?xml version='1.0'?>");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value);
break;
case XmlNodeType.EntityReference:
Console.Write(reader.Name);
break;
case XmlNodeType.EndElement:
Console.Write("</{0}>", reader.Name);
break;
}
}
}
finally
{
if (req != null)
req.Close();
}
回答by Fung
Should be able to do it fairly easily. You'll need to get the response stream by calling Response.GetResponseStream() and then use the async ResponseStream.BeginRead() in a loop.
应该可以很容易地做到这一点。您需要通过调用 Response.GetResponseStream() 来获取响应流,然后在循环中使用异步 ResponseStream.BeginRead()。
There is no Timeout setting on the Response but if you're consistently getting sent data then it should be fine.
Response 上没有超时设置,但如果您一直收到发送的数据,那么应该没问题。