How to get string from Http Request

By Emanuele Bartolesi at February 24, 2010 18:05
Filed Under: Asp.net, C#, WebServices

This HOW TO describes one of the various built-in methods .NET provides to use XML returned by a web service.
The simplest way to view the returned data is to get the response stream and put it into a string. This is especially handy for debugging. The following code gets a web page and put the the contents in a string.

        public static string GetPageAsString(string address)
        {
            string result = string.Empty;

            // Create the web request 
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
           
            // Get the web response 
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;

            // Get the stream from response
            StreamReader reader = new StreamReader(response.GetResponseStream());

            // Read the whole contents and return as a string 
            result = reader.ReadToEnd();

            reader.Dispose();
            reader = null;

            request = null;

            return result;
        }

To call this method write this code:

 GetPageAsString("http://weather.yahooapis.com/forecastrss?p=ITXX0042&u=f");


Here there is the complete sourcecode.

GetXmlFromWebIntoString.rar (37.00 kb)

About me

I will also give you some useful tips, based on the modest wisdom gained during the years that I've worked as a developer and project manager.

Widget

Ohloh profile for Emanuele Bartolesi

 

Wakoopa

Software tracking

 

Software tracking

from Amazon



hacker emblem



Scarica il pdf di Game
Rivista Game di videogiochi
Trucchi videogiochi