Исключение Apple Vailidating Receipt (существующее соединение было принудительно закрыто удаленным хостом) - PullRequest
2 голосов
/ 03 апреля 2020

У меня проблема с проверкой квитанции в песочнице. Устройство отправляет квитанцию ​​моей службе приложений Azure, которая отправляет запрос POST на сервер Apple Receipt Vaildation. Тем не менее, ~ 50% запросов я получаю сообщение об ошибке:

System.Net.WebException: The SSL connection could not be established, see inner exception. Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host

Остальные 50% работают правильно.

Код C#:

[HttpPost("{isSandbox}")]
        public bool Post(bool isSandbox, [FromBody] myReceipt myReceipt)
        {
            try
            {
                var json = new JObject(new JProperty("receipt-data", myReceipt.ReceiptString),
                           new JProperty("password", myReceipt.Password),
                           new JProperty("exclude-old-transactions", "true")).ToString();

                ASCIIEncoding ascii = new ASCIIEncoding();
                byte[] postBytes = Encoding.UTF8.GetBytes(json);

                var url = "https://buy.itunes.apple.com/verifyReceipt";
                if(isSandbox)
                {
                    url = "https://sandbox.itunes.apple.com/verifyReceipt";
                }
                var request = WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/json";
                request.ContentLength = postBytes.Length;

                using (var stream = request.GetRequestStream())
                {
                    stream.Write(postBytes, 0, postBytes.Length);
                    stream.Flush();
                }

                var sendresponse = request.GetResponse();

                string sendresponsetext = "";
                using (var streamReader = new StreamReader(sendresponse.GetResponseStream()))
                {
                    sendresponsetext = streamReader.ReadToEnd().Trim();
                }
                var responseObject = JsonConvert.DeserializeObject<responseBody>(sendresponsetext);
                if (responseObject.Latest_receipt_info[0].Expires_date == null)
                {
                    return false;
                }
                DateTime expireDate = Convert.ToDateTime(responseObject.Latest_receipt_info[0].Expires_date.Replace(" Etc/GMT", ""));
                if (expireDate >= DateTime.Now || responseObject.Latest_receipt_info[0].Is_trial_period == "true")
                {
                    // User has premium
                    return true;
                }
                return false;
            }
            catch (Exception error)
            {
                telemetry.TrackEvent("Exeption: " + error);
                return false;
            }
        }

Я получил этот код по большей части из: проверки iOS в квитанции о покупке приложения с C#

Проблема в том, что для меня это не 100 % надежный. Есть идеи, почему это происходит? Это происходит только для проверки в песочнице?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...