Search...

Wednesday, July 18, 2012

How to use HttpWebRequest and HttpWebResponse in C#


using System;
using System.IO;
using System.Net;
using System.Text;

namespace WebRequesTutorial
{
    class Program
    {
        static void Main(string[] args)
        {

            String url = @"http://msdn.microsoft.com";

            Console.WriteLine("Waiting for responce from {0}", url);

            // Create a request for the URL.
            WebRequest webRequest = WebRequest.Create(url);

            // If required by the server, set the credentials.
            webRequest.Credentials = CredentialCache.DefaultCredentials;

            // Get the response.
            HttpWebResponse httpWebResponse = webRequest.GetResponse() as HttpWebResponse;

            // Display the response status.
            Console.WriteLine("Status Description\t:{0}", httpWebResponse.StatusDescription);
            Console.WriteLine("Content Length\t:{0}", httpWebResponse.ContentLength);
            Console.WriteLine("Content Type\t:{0}", httpWebResponse.ContentType);
            Console.WriteLine("Cookies\t:{0}", httpWebResponse.Cookies);
            Console.WriteLine("Headers\t:{0}", httpWebResponse.Headers);
            Console.WriteLine("Is From Cache\t:{0}", httpWebResponse.IsFromCache);
            Console.WriteLine("Is Mutually Authenticated\t:{0}", httpWebResponse.IsMutuallyAuthenticated);
            Console.WriteLine("Last Modified\t:{0}", httpWebResponse.LastModified);
            Console.WriteLine("Method\t:{0}", httpWebResponse.Method);
            Console.WriteLine("Protocol Version\t:{0}", httpWebResponse.ProtocolVersion);
            Console.WriteLine("Response Uri\t:{0}", httpWebResponse.ResponseUri);
            Console.WriteLine("Server\t:{0}", httpWebResponse.Server);
            Console.WriteLine("Status Code\t:{0}", httpWebResponse.StatusCode);
            Console.WriteLine("Status Description\t:{0}", httpWebResponse.StatusDescription);

            Console.WriteLine("Press any key read response..........");
            Console.Read();

            // Get the stream containing content returned by the server.
            Stream dataStream = httpWebResponse.GetResponseStream();

            // Open the stream using a StreamReader for easy access.
            StreamReader streamReader = new StreamReader(dataStream);

            // Read the content.
            String responseFromServer = streamReader.ReadToEnd();

            // Write the content.
            Console.WriteLine(responseFromServer);

            using (StreamWriter streamWriter = new StreamWriter("D:\\index.html"))
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (var item in responseFromServer.Split('\n'))
                {
                    stringBuilder.AppendLine(item);
                }
                streamWriter.Write(stringBuilder.ToString());
            }

            // Cleanup the streams and the response.
            streamReader.Close();
            dataStream.Close();
            streamReader.Close();

            Console.Read();
        }
    }
}

No comments: