Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp ASP.NET Web Request POST/GET HTTPS Ignore Certificate Validation

ASP.NET Web Request POST/GET HTTPS Ignore Certificate Validation

To make a web request to a remote URL via HTTPS protocol, it’s required more works than via HTTP because it need to validate the SSL Certificate. If you have any issue with the certificate (invalid, expired or unstrusted), you programming may doesn’t work. Likely when you browse a particular website with HTTPS, you could be notified some thing: “There is a problem with this website’s security certificate.“, “This Connection is Untrusted” or “The site’s security certificate is not trusted!”

IE SSL Certificate Issue

IE SSL Certificate Issue

Therefore, the easiest solution is accepting all SSL certificate although it may less secure but most importantly, it works 🙂

Example post data to HTTPS in asp.net

An example below will try to send a post request to a remote website via a HTTPS URL, it will accept all certificates to make sure it works.

using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
 
public partial class https_request : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string url = "https://4rapiddev.com";
        Response.Write(MakeRequest(url));
    }
 
    private static bool ValidateRemoteCertificate(
    object sender,
        X509Certificate certificate,
        X509Chain chain,
        SslPolicyErrors policyErrors
    )
    {
        return true;
    }
 
    private static string MakeRequest(string uri)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
        webRequest.AllowAutoRedirect = true;
        webRequest.Method = WebRequestMethods.Http.Post;
 
        ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(
            ValidateRemoteCertificate
        );
 
        string output = "";
 
        HttpWebResponse response = null;
        try
        {
            response = (HttpWebResponse)webRequest.GetResponse();
 
            StreamReader reader = new StreamReader(response.GetResponseStream());
            output = reader.ReadToEnd();
            response.Close();
        }
        finally
        {
            if (response != null)
                response.Close();
        }
 
        return output;
    }
}

using System; using System.IO; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; public partial class https_request : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string url = "https://4rapiddev.com"; Response.Write(MakeRequest(url)); } private static bool ValidateRemoteCertificate( object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors ) { return true; } private static string MakeRequest(string uri) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); webRequest.AllowAutoRedirect = true; webRequest.Method = WebRequestMethods.Http.Post; ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback( ValidateRemoteCertificate ); string output = ""; HttpWebResponse response = null; try { response = (HttpWebResponse)webRequest.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); output = reader.ReadToEnd(); response.Close(); } finally { if (response != null) response.Close(); } return output; } }

Sep 22, 2011Hoan Huynh
ASP.NET Web Request Post/Get HTTP ExampleHow To Ask People To Like Your Facebook Page On The Landing Page
You Might Also Like:
  • ASP.NET Web Request Post/Get HTTP Example
  • PHP CURL Post To HTTPS Website
  • Setup Free SSL Certificate For Testing On Development Environment In Windows IIS 7
  • ASP.NET Cookie Domains HTTP HTTPS
  • JavaScript Detect Protocol (HTTP/ HTTPS) To Load Or Handle Accordingly
  • ASP.Net C# Download Or Save Image File From URL
  • Asp.net C# Create MD5 Hash
  • Fatal error: Uncaught CurlException: 60: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed thrown
  • HTTP/ HTTPS Document, CGI-BIN And Access/ Error Logs On cPanel, Plesk And VirtualMin
  • C# Check Google PageRank – ASP.NET Example
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago CSharpHttpWebRequest, HttpWebResponse, SslPolicyErrors, StreamReader, X509Certificate, X509Chain1,697
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,442 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,823 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,631 views
JQuery Allow only numeric characters or only alphabet characters in textbox
14,979 views
C# Read Json From URL And Parse/Deserialize Json
11,692 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development