【发布时间】:2021-09-10 15:44:55
【问题描述】:
我尝试使用 http2 请求向 apns 服务器发出请求,但收到此错误消息:
目前仅支持 HTTP/1.0 和 HTTP/1.1 版本请求。\r\n参数名称:值
我的要求:
public static async Task<string> SendManualApns(int type, long? requestId, string title, string message, int badge, string deviceToken)
{
var cert = new X509Certificate2(HttpContext.Current.Server.MapPath(ApnCertPath), ApnCertPassword, X509KeyStorageFlags.MachineKeySet);
var req = new HttpRequestMessage(HttpMethod.Post, "https://api.sandbox.push.apple.com/3/device/" + deviceToken);
req.Version = new Version(2, 0);
req.Headers.Add("apns-topic", cert.Subject.Substring(cert.Subject.LastIndexOf('=') + 1));
string sound = type == (short)NotificationType.RequestTechnicianNow ? "spaceBell.caf" : "default";
string category = type == (short)NotificationType.RequestTechnicianNow ? "nowRequestCategory" : "none";
req.Content = new StringContent("{\"aps\":{\"alert\":{\"title\":\"" + title + "\",\"body\":\"" + message + "\"},\"badge\":" + badge + ",\"sound\":\"" + sound + "\",\"requestId\":" + requestId + ",\"type\":" + type + ",\"category\":\"" + category + "\"}}");
var handler = new HttpClientHandler { ClientCertificateOptions = ClientCertificateOption.Manual };
handler.ClientCertificates.Add(cert);
var response = await new HttpClient(handler).SendAsync(req);
string content = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
return "APNS error " + response.StatusCode + ": " + content;
return "success";
}
我在 asp.net mvc webapi 2 项目中使用 .netframework 4.7.2
我使用的是 pushsharp,但它停止了,因为它还不支持 http2
【问题讨论】:
-
是客户端出错还是服务器响应出错?
-
我认为它在客户端
-
这里有很多有趣的 cmets/answers:stackoverflow.com/q/32685151/23354 - 然而,与大多数事情一样:最简单的答案可能是“更新到 .NET Core 3.1 或 .NET 5”(或更高版本) )
-
我已经升级到 .net 4.7.2 并且它支持 http2,我无法将整个项目升级到 net core
标签: c# asp.net-mvc asp.net-web-api push-notification apple-push-notifications