I just tried this myself, and he gave me an answer of 200 OK, but no content - the length of the content was 0. Are you sure this gives you the content? Anyway, I assume that you really have content.
Getting the actual text inversely depends on the knowledge of the encoding, which can be difficult. It should be in the Content-Type header, but then you have to parse it, etc.
However, if it is actually XML (for example, from http://google.com/xrds/xrds.xml "), it is much simpler. Just load the XML into memory, for example via LINQ to XML. For example:
using System; using System.IO; using System.Net; using System.Xml.Linq; using System.Web; class Test { static void Main() { string url = "http://google.com/xrds/xrds.xml"; HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url); XDocument doc; using (WebResponse response = request.GetResponse()) { using (Stream stream = response.GetResponseStream()) { doc = XDocument.Load(stream); } }
If the content is XML, getting the result in the XML object model (whether it be XDocument , XmlDocument or XmlReader ) is likely to be more valuable than plain text.
Jon Skeet Jul 17 '10 at 21:08 2010-07-17 21:08
source share