as

Friday, 28 February 2014

How to send HTTP GET Request to server in C#?

Below example shows how we can create HTTP GET request in C#.

Remember that you will have to import below namespace.
using System.Net;                


                //create HTTP request. By default it is GET request

                HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create("https://www.google.com");


                //set the proxy server and authentication info for this request

                WebProxy myproxy = new WebProxy("proxyserver:8080", true);
                myproxy.Credentials = new NetworkCredential("userId", "Password");
               getRequest.Proxy = myproxy;

                //CookieContainer for maintaining session
                ck = new CookieContainer();
                getRequest.CookieContainer = ck;

       
                //send the request and get Response from server
                HttpWebResponse response = (HttpWebResponse)getRequest.GetResponse();
                Stream datastream = response.GetResponseStream();
                StreamReader reader = new StreamReader(datastream);

                String htmlResponse = reader.ReadToEnd();

Console.WriteLine(" Response of the server is -> " + htmlResponse );



What do you think on this topic? Please express your opinion through comment below. You can write to me at reply2sagar@gmail.com

Sponsored Links

Popular Posts

Comments

ShareThis