使用C#在应用购买收据中验证iOS

编程入门 行业动态 更新时间:2024-10-09 10:27:12
本文介绍了使用C#在应用购买收据中验证iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用C#网络服务在我的服务器上验证我的ios in app purchase收据

I am verifying my ios in app purchase receipt on my server using C# web service

通过在Xcode中执行以下操作,我得到了字符串形式的收据:

I got receipt as string by doing below in Xcode:

- (void) completeTransaction: (SKPaymentTransaction *)transaction { NSString* receiptString = [[NSString alloc] initWithString:transaction.payment.productIdentifier]; NSLog(@"%@",receiptString); NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL]; NSData *receipt = [NSData dataWithContentsOfURL:receiptURL]; NSString *jsonObjectString = [receipt base64EncodedStringWithOptions:0]; }

,然后将该字符串(收据)作为参数发送到我的C# Web服务. 这是我的Web服务方法:

and I am sending that string(receipt) to my C# web service as parameter. Here is my web service method:

[WebMethod(Description = "Purchase Item Verify")] public string PurchaseItem(string receiptData) { string returnmessage = ""; try { var json = "{ 'receipt-data': '" + receiptData + "'}"; ASCIIEncoding ascii = new ASCIIEncoding(); byte[] postBytes = Encoding.UTF8.GetBytes(json); HttpWebRequest request; request = WebRequest.Create("sandbox.itunes.apple/verifyReceipt") as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/json"; request.ContentLength = postBytes.Length; Stream postStream = request.GetRequestStream(); postStream.Write(postBytes, 0, postBytes.Length); postStream.Close(); var sendresponse = (HttpWebResponse)request.GetResponse(); string sendresponsetext = ""; using (var streamReader = new StreamReader(sendresponse.GetResponseStream())) { sendresponsetext = streamReader.ReadToEnd(); } returnmessage = sendresponsetext; } catch (Exception ex) { ex.Message.ToString(); } return returnmessage; }

它总是返回{"status":21002}. 我已经搜寻了两天,但仍然找不到解决方法.有人可以帮我吗,我怎么了?

It always return {"status":21002}. I have been searching for two days , but still can't find out the solution. Can someone help me, what am i wrong ?

**我正在沙盒上进行测试,这就是为什么我使用沙盒URL.我可以在我的应用程序中验证交易收据.

**I am testing on sandbox that is why i use sandbox URL. I can verify the transaction receipt within my app.

推荐答案

我有解决办法

最适合我的代码是:

public string PurchaseItem(string receiptData) { string returnmessage = ""; try { // var json = "{ 'receipt-data': '" + receiptData + "'}"; var json = new JObject(new JProperty("receipt-data", receiptData)).ToString(); ASCIIEncoding ascii = new ASCIIEncoding(); byte[] postBytes = Encoding.UTF8.GetBytes(json); // HttpWebRequest request; var request = System.Net.HttpWebRequest.Create("sandbox.itunes.apple/verifyReceipt"); request.Method = "POST"; request.ContentType = "application/json"; request.ContentLength = postBytes.Length; //Stream postStream = request.GetRequestStream(); //postStream.Write(postBytes, 0, postBytes.Length); //postStream.Close(); using (var stream = request.GetRequestStream()) { stream.Write(postBytes, 0, postBytes.Length); stream.Flush(); } // var sendresponse = (HttpWebResponse)request.GetResponse(); var sendresponse = request.GetResponse(); string sendresponsetext = ""; using (var streamReader = new StreamReader(sendresponse.GetResponseStream())) { sendresponsetext = streamReader.ReadToEnd().Trim(); } returnmessage = sendresponsetext; } catch (Exception ex) { ex.Message.ToString(); } return returnmessage;

只花两天半的时间来更改一种方法.谢谢上帝.

Spending two and half days just to change a method. Thanks GOD.

更多推荐

使用C#在应用购买收据中验证iOS

本文发布于:2023-11-26 09:10:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633506.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:收据   iOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!