I'm having a problem with validating my sandbox receipt. The device sends the receipt to my Azure App Service, which makes a POST request to the Apple Receipt Vaildation server. However, ~50% of the requests, I get an error back saying:
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
The other 50% does work correctly.
The C# code:
[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;
}
}
I got this code for the most part from: verifying iOS in app purchase receipt with C#
The problem is that for me it isn't a 100% reliable. Any idea of why this is happening? Is this only happening for sandbox validation?