runtime: APNs + HTTP/2 + HttpClient + ClientCertificate throws WinHttpException
I’m trying to sent message to APNs using HttpClient and p12 certificate.
Unfortunatelly I’m getting The server returned an invalid or unrecognized response error on SendAsync.
Here is Code Example:
public async Task SendAsync(ApnsHttp2Notification notification)
{
var url = string.Format("https://{0}:{1}/3/device/{2}",
_options.Host,
_options.Port,
notification.DeviceToken);
var uri = new Uri(url);
using (var certificate = SecurityHelperMethods.GetCertificateFromFile(_options.CertificateFileName, _options.CertificatePassword))
using (var httpHandler = new HttpClientHandler { SslProtocols = SslProtocols.Tls12 })
{
httpHandler.ClientCertificates.Add(certificate);
using (var httpClient = new HttpClient(httpHandler, true))
using (var request = new HttpRequestMessage(HttpMethod.Post, url))
{
request.Content = new StringContent("Test");
request.Version = new Version(2, 0);
try
{
using (var httpResponseMessage = await httpClient.SendAsync(request))
{
var responseContent = await httpResponseMessage.Content.ReadAsStringAsync();
var result = $"status: {(int)httpResponseMessage.StatusCode} content: {responseContent}";
}
}
catch (Exception e)
{
throw;
}
}
}
}
Exception:
{System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: The write operation failed, see inner exception. ---> System.Net.Http.WinHttpException: The server returned an invalid or unrecognized response
--- End of inner exception stack trace ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpContent.<CopyToAsyncCore>d__44.MoveNext()
--- End of inner exception stack trace ---
at System.Net.Http.HttpContent.<CopyToAsyncCore>d__44.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.WinHttpHandler.<InternalSendRequestBodyAsync>d__131.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at APNs.ApnService.<SendAsync>d__4.MoveNext() in D:\projects\ipl\src\Services\Notifiers\IPL.APNsNotifier.Worker\APNs\ApnService.cs:line 69}
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146232800
HelpLink: null
InnerException: {System.IO.IOException: The write operation failed, see inner exception. ---> System.Net.Http.WinHttpException: The server returned an invalid or unrecognized response
--- End of inner exception stack trace ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpContent.<CopyToAsyncCore>d__44.MoveNext()}
Message: "Error while copying content to a stream."
Source: "System.Net.Http"
StackTrace: " at System.Net.Http.HttpContent.<CopyToAsyncCore>d__44.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Net.Http.WinHttpHandler.<InternalSendRequestBodyAsync>d__131.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Sys
tem.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at APNs.ApnService.<SendAsync>d__4.MoveNext() in ...\\ApnService.cs:line 69"
TargetSite: {Void MoveNext()}
Target Framework: netcoreapp2.0 OS: Windows 10 OS Version: 1703 OS Build: 15063.332
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 6
- Comments: 32 (8 by maintainers)
Yes @cgyan009 this is the code i use. If using .NET Framework you have to get the System.Net.Http.WinHttpHandler (4.4x) nuget package (.NET Core supports HTTP2)
This issue is tracking only WinHttpHandler. So, the ‘tracking-external-issue’ label is appropriate.
This issue is not tracking SocketsHttpHandler work to enable this scenario. SocketsHttpHandler currently doesn’t support HTTP/2.0. So, the scenario is not even possible in SocketsHttpHandler. A future version of .NET Core will update SocketsHttpHandler to support HTTP/2.0.
Maybe a slightly improved and 100% working APN wrapper: