51

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

How to send an HTTPS GET Request in C#?

3 Answers 3

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();
3
  • 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 1, 2014 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 9, 2018 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: learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient
    – Michiel
    Aug 13, 2021 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;
    }

}
2
  • 2
    Will this be a http call or an https call?
    – Yituo
    Oct 5, 2018 at 0:13
  • 1
    This will result in an exception saying SLS/TLS issue. Nov 7, 2020 at 6:55
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/

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.