本文整理汇总了C#中System.Net.Http.WinHttpHandler类的典型用法代码示例。如果您正苦于以下问题:C# WinHttpHandler类的具体用法?C# WinHttpHandler怎么用?C# WinHttpHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WinHttpHandler类属于System.Net.Http命名空间,在下文中一共展示了WinHttpHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AutomaticRedirection_SetFalseAndGet_ValueIsFalse
public void AutomaticRedirection_SetFalseAndGet_ValueIsFalse()
{
var handler = new WinHttpHandler();
handler.AutomaticRedirection = false;
Assert.False(handler.AutomaticRedirection);
}
示例2: GetAsync_RedirectResponseHasCookie_CookieSentToFinalUri
public async Task GetAsync_RedirectResponseHasCookie_CookieSentToFinalUri(
CookieUsePolicy cookieUsePolicy,
string cookieName,
string cookieValue)
{
Uri uri = Configuration.Http.RedirectUriForDestinationUri(false, 302, Configuration.Http.RemoteEchoServer, 1);
var handler = new WinHttpHandler();
handler.WindowsProxyUsePolicy = WindowsProxyUsePolicy.UseWinInetProxy;
handler.CookieUsePolicy = cookieUsePolicy;
if (cookieUsePolicy == CookieUsePolicy.UseSpecifiedCookieContainer)
{
handler.CookieContainer = new CookieContainer();
}
using (HttpClient client = new HttpClient(handler))
{
client.DefaultRequestHeaders.Add(
"X-SetCookie",
string.Format("{0}={1};Path=/", cookieName, cookieValue));
using (HttpResponseMessage httpResponse = await client.GetAsync(uri))
{
string responseText = await httpResponse.Content.ReadAsStringAsync();
_output.WriteLine(responseText);
Assert.True(JsonMessageContainsKeyValue(responseText, cookieName, cookieValue));
}
}
}
示例3: Ctor_ExpectedDefaultPropertyValues
public void Ctor_ExpectedDefaultPropertyValues()
{
var handler = new WinHttpHandler();
Assert.Equal(SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12, handler.SslProtocols);
Assert.Equal(true, handler.AutomaticRedirection);
Assert.Equal(50, handler.MaxAutomaticRedirections);
Assert.Equal(DecompressionMethods.Deflate | DecompressionMethods.GZip, handler.AutomaticDecompression);
Assert.Equal(CookieUsePolicy.UseInternalCookieStoreOnly, handler.CookieUsePolicy);
Assert.Equal(null, handler.CookieContainer);
Assert.Equal(null, handler.ServerCertificateValidationCallback);
Assert.Equal(false, handler.CheckCertificateRevocationList);
Assert.Equal(ClientCertificateOption.Manual, handler.ClientCertificateOption);
X509Certificate2Collection certs = handler.ClientCertificates;
Assert.True(certs.Count == 0);
Assert.Equal(false, handler.PreAuthenticate);
Assert.Equal(null, handler.ServerCredentials);
Assert.Equal(WindowsProxyUsePolicy.UseWinHttpProxy, handler.WindowsProxyUsePolicy);
Assert.Equal(CredentialCache.DefaultCredentials, handler.DefaultProxyCredentials);
Assert.Equal(null, handler.Proxy);
Assert.Equal(Int32.MaxValue, handler.MaxConnectionsPerServer);
Assert.Equal(TimeSpan.FromSeconds(60), handler.ConnectTimeout);
Assert.Equal(TimeSpan.FromSeconds(30), handler.SendTimeout);
Assert.Equal(TimeSpan.FromSeconds(30), handler.ReceiveHeadersTimeout);
Assert.Equal(TimeSpan.FromSeconds(30), handler.ReceiveDataTimeout);
Assert.Equal(64 * 1024, handler.MaxResponseHeadersLength);
Assert.Equal(64 * 1024, handler.MaxResponseDrainSize);
}
示例4: CheckCertificateRevocationList_SetTrue_ExpectedWinHttpHandleSettings
public void CheckCertificateRevocationList_SetTrue_ExpectedWinHttpHandleSettings()
{
var handler = new WinHttpHandler();
SendRequestHelper(handler, delegate { handler.CheckCertificateRevocationList = true; });
Assert.True(APICallHistory.WinHttpOptionEnableSslRevocation.Value);
}
示例5: NoCallback_ValidCertificate_CallbackNotCalled
public async Task NoCallback_ValidCertificate_CallbackNotCalled()
{
var handler = new WinHttpHandler();
using (var client = new HttpClient(handler))
{
HttpResponseMessage response = await client.GetAsync(HttpTestServers.SecureRemoteEchoServer);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.False(_validationCallbackHistory.WasCalled);
}
}
示例6: SendAsync_SlowServerRespondsAfterDefaultReceiveTimeout_ThrowsHttpRequestException
public void SendAsync_SlowServerRespondsAfterDefaultReceiveTimeout_ThrowsHttpRequestException()
{
var handler = new WinHttpHandler();
using (var client = new HttpClient(handler))
{
Task<HttpResponseMessage> t = client.GetAsync(SlowServer);
AggregateException ag = Assert.Throws<AggregateException>(() => t.Wait());
Assert.IsType<HttpRequestException>(ag.InnerException);
}
}
示例7: SendAsync_SimpleGet_Success
public void SendAsync_SimpleGet_Success()
{
var handler = new WinHttpHandler();
var client = new HttpClient(handler);
// TODO: This is a placeholder until GitHub Issue #2383 gets resolved.
var response = client.GetAsync(HttpTestServers.RemoteGetServer).Result;
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var responseContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
_output.WriteLine(responseContent);
}
示例8: UseCallback_NotSecureConnection_CallbackNotCalled
public async Task UseCallback_NotSecureConnection_CallbackNotCalled()
{
var handler = new WinHttpHandler();
handler.ServerCertificateValidationCallback = CustomServerCertificateValidationCallback;
using (var client = new HttpClient(handler))
{
HttpResponseMessage response = await client.GetAsync(HttpTestServers.RemoteGetServer);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.False(_validationCallbackHistory.WasCalled);
}
}
示例9: AutomaticRedirection_SetTrue_ExpectedWinHttpHandleSettings
public void AutomaticRedirection_SetTrue_ExpectedWinHttpHandleSettings()
{
var handler = new WinHttpHandler();
SendRequestHelper.Send(
handler,
delegate { handler.AutomaticRedirection = true; });
Assert.Equal(
Interop.WinHttp.WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP,
APICallHistory.WinHttpOptionRedirectPolicy);
}
示例10: AutomaticRedirection_SetFalse_ExpectedWinHttpHandleSettings
public void AutomaticRedirection_SetFalse_ExpectedWinHttpHandleSettings()
{
var handler = new WinHttpHandler();
SendRequestHelper(
handler,
delegate { handler.AutomaticRedirection = false; });
Assert.Equal(
Interop.WinHttp.WINHTTP_OPTION_REDIRECT_POLICY_NEVER,
APICallHistory.WinHttpOptionRedirectPolicy);
}
示例11: UseCallback_ValidCertificate_ExpectedValuesDuringCallback
public async Task UseCallback_ValidCertificate_ExpectedValuesDuringCallback()
{
var handler = new WinHttpHandler();
handler.ServerCertificateValidationCallback = CustomServerCertificateValidationCallback;
using (var client = new HttpClient(handler))
{
HttpResponseMessage response = await client.GetAsync(HttpTestServers.SecureRemoteEchoServer);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.True(_validationCallbackHistory.WasCalled);
ConfirmValidCertificate(HttpTestServers.Host);
}
}
示例12: SendAsync_SlowServerAndCancel_ThrowsTaskCanceledException
public async Task SendAsync_SlowServerAndCancel_ThrowsTaskCanceledException()
{
var handler = new WinHttpHandler();
using (var client = new HttpClient(handler))
{
var cts = new CancellationTokenSource();
Task<HttpResponseMessage> t = client.GetAsync(SlowServer, cts.Token);
await Task.Delay(500);
cts.Cancel();
AggregateException ag = Assert.Throws<AggregateException>(() => t.Wait());
Assert.IsType<TaskCanceledException>(ag.InnerException);
}
}
示例13: UseCallback_RedirectandValidCertificate_ExpectedValuesDuringCallback
public async Task UseCallback_RedirectandValidCertificate_ExpectedValuesDuringCallback()
{
Uri uri = Configuration.Http.RedirectUriForDestinationUri(true, 302, Configuration.Http.SecureRemoteEchoServer, 1);
var handler = new WinHttpHandler();
handler.ServerCertificateValidationCallback = CustomServerCertificateValidationCallback;
using (var client = new HttpClient(handler))
{
HttpResponseMessage response = await client.GetAsync(uri);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.True(_validationCallbackHistory.WasCalled);
ConfirmValidCertificate(Configuration.Http.Host);
}
}
示例14: CanReadAndWriteWithHttpsConnectionFilter
public async Task CanReadAndWriteWithHttpsConnectionFilter()
{
RemoteCertificateValidationCallback validationCallback =
(sender, cert, chain, sslPolicyErrors) => true;
try
{
#if DNX451
var handler = new HttpClientHandler();
ServicePointManager.ServerCertificateValidationCallback += validationCallback;
#else
var handler = new WinHttpHandler();
handler.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
#endif
var serverAddress = "https://localhost:54321/";
var serviceContext = new TestServiceContext()
{
ConnectionFilter = new HttpsConnectionFilter(
new HttpsConnectionFilterOptions
{ ServerCertificate = new X509Certificate2(@"TestResources/testCert.pfx", "testPassword")},
new NoOpConnectionFilter())
};
using (var server = new TestServer(App, serviceContext, serverAddress))
{
using (var client = new HttpClient(handler))
{
var result = await client.PostAsync(serverAddress, new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("content", "Hello World?")
}));
Assert.Equal("content=Hello+World%3F", await result.Content.ReadAsStringAsync());
}
}
}
finally
{
#if DNX451
ServicePointManager.ServerCertificateValidationCallback -= validationCallback;
#endif
}
}
示例15: SendSmsAsync
/// <summary>
/// Sends an SMS message.
/// </summary>
/// <param name="message">SMS message.</param>
/// <returns>Task.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="message"/> is <c>null</c>.</exception>
public async Task SendSmsAsync(SmsMessage message)
{
if (message == null) throw new ArgumentNullException(nameof(message));
var url = ComposeUrl(message);
#if NET45
var messageHandler = new WebRequestHandler();
messageHandler.ClientCertificates.Add(SmsConnectorConfiguration.Certificate);
#else
var messageHandler = new WinHttpHandler();
messageHandler.ClientCertificates.Add(SmsConnectorConfiguration.Certificate);
#endif
using (var client = new HttpClient(messageHandler))
{
var response = await client.GetAsync(url).ConfigureAwait(false);
await HandleErrorResponse(response).ConfigureAwait(false);
}
}