Read response text

HttpWebRequest request = WebRequest.Create("http://google.com") as HttpWebRequest; request .Accept = "application/xrds+xml"; HttpWebResponse response = (HttpWebResponse)request .GetResponse(); WebHeaderCollection header= response.Headers; 

Here google returns the text. How to read it?

+47
Jul 17 '10 at 20:45
source share
6 answers

Your "application/xrds+xml" giving me problems, I was getting Content-Length from 0 (no response).

After that, you can access the response using response.GetResponseStream() .

  HttpWebRequest request = WebRequest.Create("http://google.com") as HttpWebRequest; //request.Accept = "application/xrds+xml"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); WebHeaderCollection header = response.Headers; var encoding = ASCIIEncoding.ASCII; using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding)) { string responseText = reader.ReadToEnd(); } 
+68
Jul 17 '10 at 20:57
source share

The accepted answer WebResponse not display WebResponse or decodes the text. In addition, there is a new way to do this in .NET 4.5.

To perform an HTTP GET and read the response text, do the following:

.NET 1.1 - 4.0

 public static string GetResponseText(string address) { var request = (HttpWebRequest)WebRequest.Create(address); using (var response = (HttpWebResponse)request.GetResponse()) { var encoding = Encoding.GetEncoding(response.CharacterSet); using (var responseStream = response.GetResponseStream()) using (var reader = new StreamReader(responseStream, encoding)) return reader.ReadToEnd(); } } 

.NET 4.5

 public static async Task<string> GetResponseText(string address) { using (var httpClient = new HttpClient()) return await httpClient.GetStringAsync(address); } 
+47
09 Feb '13 at 4:19
source share

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); } } // Now do whatever you want with doc here Console.WriteLine(doc); } } 

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.

+8
Jul 17 '10 at 21:08
source share
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.google.com"); request.Method = "GET"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string strResponse = reader.ReadToEnd(); 
+2
Dec 18 '15 at 16:54
source share

response.GetResponseStream() should be used to return a response stream. And don't forget the close Stream and Response objects.

+1
Jul 17 '10 at 20:55
source share

This article provides a good overview of using the HttpWebResponse object: How to use HttpWebResponse

The relevant bits are below:

  HttpWebResponse webresponse; webresponse = (HttpWebResponse)webrequest.GetResponse(); Encoding enc = System.Text.Encoding.GetEncoding(1252); StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(),enc); string Response = loResponseStream.ReadToEnd(); loResponseStream.Close(); webresponse.Close(); return Response; 
+1
Jul 17 '10 at 20:57
source share



All Articles