How do I use WebRequest to access an SSL encrypted site using HTTPS?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
To access an HTTPS site with WebRequest (or HttpWebRequest) in C# .NET, you typically just use an https:// URL and .NET handles TLS negotiation automatically. However, issues arise when the server uses a self-signed certificate, an expired certificate, or when the .NET framework's default TLS version is too old. The most common fix is setting ServicePointManager.SecurityProtocol to TLS 1.2 or higher. For modern .NET (5+), consider using HttpClient instead of WebRequest, as WebRequest is obsolete.
Basic HTTPS Request
If the server has a valid certificate from a trusted CA, this works without any additional configuration.
Fix: Set TLS Version
Older .NET Framework versions (4.5 and below) default to SSL 3.0 / TLS 1.0, which many servers reject. Set the protocol explicitly:
Handling Self-Signed Certificates
Never use this in production. It disables certificate validation, making the connection vulnerable to man-in-the-middle attacks.
Validate a Specific Certificate
Adding Client Certificates
Some APIs require mutual TLS (mTLS) — the client presents a certificate to the server:
POST Request Over HTTPS
Modern Alternative: HttpClient
WebRequest is obsolete in .NET 6+. Use HttpClient instead:
Common Pitfalls
- Not setting
SecurityProtocolbefore the first request:ServicePointManager.SecurityProtocolmust be set before anyWebRequestis created. Setting it after the first request has no effect on already-established connections. Set it at application startup. - Using
ServerCertificateValidationCallback = truein production: Accepting all certificates disables TLS security entirely. An attacker can intercept traffic with any certificate. Only use this for local development with self-signed certificates. - Forgetting that
WebRequestis obsolete: In .NET 6+,WebRequestandHttpWebRequestare marked as obsolete. UseHttpClientfor new code.HttpClientis also more efficient because it reuses connections. - Creating a new
HttpClientper request:HttpClientis designed to be reused. Creating a new instance per request exhausts socket connections (socket exhaustion). Use a singleHttpClientinstance orIHttpClientFactoryin ASP.NET Core. - TLS errors with older Windows versions: Windows 7 and Server 2008 R2 do not support TLS 1.2 by default without a Windows update. If your application runs on old OS versions, ensure the TLS 1.2 update (KB3140245) is installed.
Summary
- Use
https://URLs withWebRequest.Create()— .NET handles TLS automatically for valid certificates - Set
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12for .NET Framework 4.5-4.6.2 - For self-signed certificates, use
ServerCertificateValidationCallback(development only) - For client certificates (mTLS), add them to
request.ClientCertificates - Migrate to
HttpClientfor .NET 6+ —WebRequestis obsolete - Always set TLS protocol and certificate callbacks at application startup, before any requests
Related reading
- How do raft nodes learn about peers?
- How do SO_REUSEADDR and SO_REUSEPORT differ?
- How do you add CloudFront in front of API Gateway
- How do you debug MVC 4 API routes?
- How do I verify that an Android apk is signed with a release certificate?
- How do parameterized queries help against SQL injection?
- How do you find the cluster service CIDR of a Kubernetes cluster?
- How do you get a Kubernetes pod's name from its IP address?
System design interview problems on this topic
- Design a Load Balancer: solution and walkthrough
- Design a Domain Name System: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.