How to send an HTTPS GET Request in C#

164,514

Solution 1

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();

Solution 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/

Solution 3

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;
    }

}
Share:
164,514
Admin
Author by

Admin

Updated on February 02, 2020

Comments

  • Admin
    Admin over 4 years

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

    How to send an HTTPS GET Request in C#?

  • Pavdro
    Pavdro over 9 years
    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
  • Bitterblue
    Bitterblue almost 6 years
    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!
  • Yituo
    Yituo over 5 years
    Will this be a http call or an https call?
  • taimoor1990
    taimoor1990 over 3 years
    This will result in an exception saying SLS/TLS issue.
  • Alexander Ryan Baggett
    Alexander Ryan Baggett about 3 years
    Post actual code, links can become stale.
  • Michiel
    Michiel almost 3 years
    Note that WebRequest and HttpWebRequest are no longer recommended to use for new projects. Microsoft suggests using the newer HttpClient class instead: docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclie‌​nt