Skip to content

Instantly share code, notes, and snippets.

@hsupu
Last active March 23, 2021 00:17
Show Gist options
  • Save hsupu/26cb7691bc711920334823f504fd93e1 to your computer and use it in GitHub Desktop.
Save hsupu/26cb7691bc711920334823f504fd93e1 to your computer and use it in GitHub Desktop.
PowerShell HttpClient ServerCertificateCustomValidationCallback demo
$cscode = @"
using System;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
public class My {
private static SHA256 sha256 = SHA256.Create();
private static string bin2hex(byte[] ba) {
var sb = new StringBuilder();
foreach (byte b in ba) {
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}
public static Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> GetServerCertificateValidationCallback(string expect = null) {
if (null == expect) {
return (sender, cert, chain, errors) => true;
}
expect = expect.ToUpper();
return (sender, cert, chain, errors) => {
var hash = sha256.ComputeHash(cert.GetRawCertData());
var actual = bin2hex(hash).ToUpper();
return string.Equals(expect, actual);
};
}
}
"@
Add-Type $cscode
$httpClientHandler = New-Object System.Net.Http.HttpClientHandler
$httpClient = New-Object System.Net.Http.HttpClient($httpClientHandler)
function http() {
param(
[Parameter(Mandatory=$true)] [System.Net.Http.HttpMethod] $method,
[Parameter(Mandatory=$true)] [System.Uri] $uri,
[Parameter(Mandatory=$false)] [Action[System.Net.Http.Headers.HttpRequestHeaders]] $headersAction,
[Parameter(Mandatory=$false)] [System.Net.Http.HttpContent] $content
)
$request = New-Object System.Net.Http.HttpRequestMessage($method, $uri)
$headersAction.Invoke($request.Headers)
$request.Content = $content
$httpClientHandler.ServerCertificateCustomValidationCallback = [My]::GetServerCertificateValidationCallback();
$httpClientHandler.SslProtocols = [System.Security.Authentication.SslProtocols]::Tls11 -bor [System.Security.Authentication.SslProtocols]::Tls12
$future = $httpClient.SendAsync($request)
$response = $future.Result
if ($null -eq $response) {
Write-Error $future.Exception
return $null
}
return $response
}
function get($url) {
$method = New-Object System.Net.Http.HttpMethod("GET")
$uri = New-Object System.Uri($url)
$headersAction = {
param([System.Net.Http.Headers.HttpRequestHeaders]$headers)
$headers.Add('Host', "cn.bing.com")
$headers.Add('User-Agent', "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36")
$headers.Add('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
$headers.Add('Accept-Language', 'en-US')
}
$content = New-Object System.Net.Http.StringContent('', [System.Text.Encoding]::UTF8)
$response = http $method $uri $headersAction $content
if ($null -eq $response) {
return -1
}
Write-Output $response.StatusCode
Write-Output $response.Content.ReadAsStringAsync().Result
$response.Dispose()
return 0
}
get "https://cn.bing.com/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment