Friday, September 2, 2016

making HTTP/HTTPS request in C#

        

private static string LoadUrl(string path)
        {
            Uri myUri = new Uri(path, UriKind.Absolute);
            string stringResponse = null;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUri);
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";

            WebResponse response = request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                stringResponse = reader.ReadToEnd();
            }

            return stringResponse;
        }