5
Contributors
5
Replies
7
Views
7 Years
Discussion Span
Last Post by sknake
0

Here is an example of using GET. The yahoo URI will succeed but the gmail address will throw an exception because the certificate is bad.

using System;
using System.IO;
using System.Net;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmSSL : Form
  {
    public frmSSL()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      //This has a valid SSL certificate
      string html1 = GET(@"https://login.yahoo.com/");
      string html2;

      try
      {
        html2 = GET(@"https://www.gmail.com");
      }
      catch (WebException Ex)
      {
        MessageBox.Show("Error" + Environment.NewLine +
          Ex.Source + Environment.NewLine +
          Ex.Message);
      }

      Console.WriteLine(html1);

      //System.Diagnostics.Debugger.Break();
    }

    private static string GET(string URL)
    {
      Uri url = new Uri(URL, UriKind.Absolute);
      HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
      objRequest.Method = "GET";

      HttpWebResponse response = (HttpWebResponse)objRequest.GetResponse();
      using (Stream resStream = response.GetResponseStream())
      {
        using (StreamReader reader = new StreamReader(resStream))
        {
          string html = reader.ReadToEnd();
          return html;
        }
      }
    }

  }
}
0

https://gmail.com did not throw the exception.

public static string FetchURL(string url)
    {
        
        const int bufSizeMax = 65536; // max read buffer size conserves memory
        const int bufSizeMin = 8192;  // min size prevents numerous small reads
        StringBuilder sb;

        // A WebException is thrown if HTTP request fails
        try
        {

            // Create an HttpWebRequest using WebRequest.Create (see .NET docs)!
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Method = WebRequestMethods.Http.Get;


            // Execute the request and obtain the response stream
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();

            // Content-Length header is not trustable, but makes a good hint.
            // Responses longer than int size will throw an exception here!
            int length = (int)response.ContentLength;

            // Use Content-Length if between bufSizeMax and bufSizeMin
            int bufSize = bufSizeMin;
            if (length > bufSize)
                bufSize = length > bufSizeMax ? bufSizeMax : length;

            // Allocate buffer and StringBuilder for reading response
            byte[] buf = new byte[bufSize];
            sb = new StringBuilder(bufSize);

            // Read response stream until end
            while ((length = responseStream.Read(buf, 0, buf.Length)) != 0)
                sb.Append(Encoding.UTF8.GetString(buf, 0, length));

            return sb.ToString();

        }
        catch (Exception ex)
        {
            sb = new StringBuilder(ex.Message);
            return sb.ToString();
        }
        
    }
0

Either way it was just showing that an exception would be thrown if you connected to a site with an invalid SSL certificate. There are a number of policy options in windows to handle the certs as well as the framework.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.