How to post json object in web service

╄→гoц情女王★ 提交于 2019-12-11 06:15:15

问题


Can any one help me please that how to post Json object to web service using c#? Actually I am trying to verify an App Store Transaction Receipt in C#.

follow this link to have an enough idea.

http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/StoreKitGuide/VerifyingStoreReceipts/VerifyingStoreReceipts.html#//apple_ref/doc/uid/TP40008267-CH104-SW1

Please help me if you have some idea or knowledge.

Thanks in advance,


回答1:


string receiptData = "(receipt bytes here)";

string url = "https://buy.itunes.apple.com/verifyReceipt";

HttpWebRequest oneWebRequest = (HttpWebRequest)WebRequest.Create(url);
oneWebRequest.Method = "POST";
oneWebRequest.ContentType = "application/json; charset=utf-8";

string json = "{ \"receipt-data\": \"" + receiptData + "\" }";

oneWebRequest.ContentLength = json.Length;
using (System.IO.StreamWriter oneStreamWriter =
  new System.IO.StreamWriter(oneWebRequest.GetRequestStream(),
                             System.Text.Encoding.ASCII))
{
    oneStreamWriter.Write(json);
}

HttpWebResponse oneWebResponse = (HttpWebResponse)oneWebRequest.GetResponse();

// ReceiptStatus is a class with two public string properties (status & receipt)
DataContractJsonSerializer oneDataContractJsonSerializer =
  new DataContractJsonSerializer(typeof(List<Product>));
ReceiptStatus oneReceiptStatus =
   (ReceiptStatus) oneDataContractJsonSerializer.ReadObject(
      oneWebResponse.GetResponseStream()
   );



回答2:


try with json.Net http://james.newtonking.com/pages/json-net.aspx



来源:https://stackoverflow.com/questions/2309954/how-to-post-json-object-in-web-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!