Skip to content

Instantly share code, notes, and snippets.

@EgorBo
Created October 14, 2019 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EgorBo/89bf60034a3592fdd989d6aecf8662e9 to your computer and use it in GitHub Desktop.
Save EgorBo/89bf60034a3592fdd989d6aecf8662e9 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Security;
using System.Text;
using System.Threading.Tasks;
static class Program
{
static HttpClientHandler CreateHttpClientHandler(bool useSocketsHttpHandler)
{
HttpClientHandler handler;
if (useSocketsHttpHandler)
{
handler = new HttpClientHandler();
}
else
{
// Create platform specific handler.
ConstructorInfo ctor = typeof(HttpClientHandler).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(bool) }, null);
handler = (HttpClientHandler)ctor.Invoke(new object[] { useSocketsHttpHandler });
}
return handler;
}
static async Task Main()
{
AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", true);
using (HttpClientHandler handler = new HttpClientHandler())
using (var client = new HttpClient(handler))
{
handler.Credentials = new NetworkCredential(
ActiveDirectoryUserName,
ActiveDirectoryUserPassword);
ServicePointManager.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
var request = new HttpRequestMessage();
var server = DomainJoinedHttpHost;
request.RequestUri = new Uri(server);
// Force HTTP/1.1 since both CurlHandler and SocketsHttpHandler have problems with
// HTTP/2.0 and Windows authentication (due to HTTP/2.0 -> HTTP/1.1 downgrade handling).
// Issue #35195 (for SocketsHttpHandler).
request.Version = new Version(1, 1);
Console.WriteLine(await client.GetStringAsync(new Uri(server)));
}
}
public static string DomainJoinedHttpHost = "http://localhost:5000/";
public static string ActiveDirectoryUserPassword = "5ty67ui8";
public static string ActiveDirectoryUserName = "bogat";
public static string ActiveDirectoryName = "WORKGROUP";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment