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();
Sign up to request clarification or add additional context in comments.

3 Comments

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
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!
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
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 Comments

Will this be a http call or an https call?
This will result in an exception saying SLS/TLS issue.
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 Comment

Post actual code, links can become stale.

Your Answer

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