51

Related: how-do-i-use-webrequest-to-access-an-ssl-encrypted-site-using-https

How to send an HTTPS GET Request in C#?

Community
  • 1
  • 1

3 Answers3

78

Add ?var1=data1&var2=data2 to the end of url to submit values to the page via GET:

using System.Net;
using System.IO;

string url = "https://www.example.com/scriptname.php?var1=hello";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
user35443
  • 6,309
  • 12
  • 52
  • 75
Kevin Newman
  • 2,437
  • 1
  • 15
  • 12
  • 5
    Note: this works for HTTP, I tested this code with HTTPS url and it didn't work. Going to try WebClient and see if that works with SSL – Pavdro Sep 01 '14 at 10:17
  • Use the address of this page. Stackoverflow runs on https and you will get a `200 OK` response `string url = "https://stackoverflow.com/questions/...";`. Worked! – Bitterblue Jul 09 '18 at 12:03
  • Note that WebRequest and HttpWebRequest are no longer recommended to use for new projects. Microsoft suggests using the newer HttpClient class instead: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient – Michiel Aug 13 '21 at 11:28
2

Simple Get Request using HttpClient Class

using System.Net.Http;

class Program
{
   static void Main(string[] args)
    {
        HttpClient httpClient = new HttpClient();
        var result = httpClient.GetAsync("https://www.google.com").Result;
    }

}
nirali
  • 39
  • 2
2

I prefer to use WebClient, it seems to handle SSL transparently:

http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx

Some troubleshooting help here:

https://clipperhouse.com/webclient-fiddler-and-ssl/

DeadlyChambers
  • 5,217
  • 4
  • 44
  • 61
Matt Sherman
  • 8,298
  • 4
  • 37
  • 57