System.Net.Request Class for HTTPS

This section describes the System.Net.Request class in .NET Framework 4 that allows you to communicate with HTTPS Web servers.

If you want to write your own C# program to communicate with an HTTPS Web server, .NET Framework 4 has built-in support for HTTP Transport Security and Public Key Infrastructure.

The easiest way to communicate with a Web server is to use the System.Net.Request class. Here is a C# test program that uses the System.Net.Request class to get a Web page from an HTTP server or an HTTPS server:

// WebReader.cs
// Copyright (c) 2011,, All Rights Reserved.
using System;
using System.IO;
using System.Text;
using System.Net;
public class WebReader {
   public static void Main(string[] args) {
      string url = args[0];
      WebRequest req = WebRequest.Create(url);
      WebResponse res = req.GetResponse();
      StreamReader web 
         = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
      string line;
      while ((line = web.ReadLine()) != null)

For the first test, I want to run this C# program to connect a normal HTTP server,

C:\herong>\windows\Microsoft.NET\Framework\v4.0.30319\csc WebReader.cs

Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.


<!DOCTYPE html>
<html lang="en-US" class="y-fp-bg y-fp-pg-grad  bkt701">
<!-- m2 template 0 -->
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

The output confirms that my test C# program,, works fine on HTTP servers.

Now let's run this program to connect the yahoo login HTTPS server, which we have used in previous tutorials with Internet Explorer and Firefox Web browsers.


<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sign in to Yahoo!</title>

Cool. The output confirms that the System.Net.WebRequest class automatically does extra security tasks required by the HTTPS communication.

Last update: 2011.

Table of Contents

 About This Book

 Introduction of PKI (Public Key Infrastructure)

 Introduction of HTTPS (Hypertext Transfer Protocol Secure)

 Using HTTPS with IE (Internet Explorer) 10

 Using HTTPS with Chrome 40

 Using HTTPS with Firefox 35

 Perl Scripts Communicating with HTTPS Servers

 PHP Scripts Communicating with HTTPS Servers

 Java Programs Communicating with HTTPS Servers

 Certificate Stores and Certificate Console

.NET Programs Communicating with HTTPS Servers

System.Net.Request Class for HTTPS

 Test with CA Certificate Disabled

 Test with Second CA Certificate Disabled

 .NET Program Failed with CA Certificates Deleted

 .NET Reporting Certificate Validation Failed - Root CA Offering Free Certificates

 PKI CA Administration - Issuing Certificates

 Digital Signature - Microsoft Word 2007

 Digital Signature - 3

 S/MIME and Email Security

 PKI (Public Key Infrastructure) Terminology

 Outdated Tutorials


 PDF Printing Version