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, HerongYang.com, 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)
         Console.WriteLine(line);
   }
}

For the first test, I want to run this C# program to connect a normal HTTP server, http://www.yahoo.com:

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.

C:\herong>WebReader.exe http://www.yahoo.com

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

  <title>Yahoo!</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
...

The output confirms that my test C# program, WebReader.java, 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.

C:\herong>WebReader.exe https://login.yahoo.com

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org...
<html>
<head>
<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

 CAcert.org - Root CA Offering Free Certificates

 PKI CA Administration - Issuing Certificates

 Digital Signature - Microsoft Word 2007

 Digital Signature - OpenOffice.org 3

 S/MIME and Email Security

 PKI (Public Key Infrastructure) Terminology

 Outdated Tutorials

 References

 PDF Printing Version