Webservice调用-我认为Json请求体是错误的

本文关键字:请求 错误 Json 调用 Webservice | 更新日期: 2023-09-27 18:02:39

我认为Json调用的REQUEST BODY是错误的。有人能帮忙吗?我知道我已经很接近了,只是似乎无法完全到达那里:

_externalBillingSubscriptionURI += externalBillingSubscriptionNumber + "/cancel";
        Uri address = new Uri(_externalBillingSubscriptionURI);
        // Create the web request  
        HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
        // Set type to Put  
        request.Method = "PUT";
        request.Headers.Add("apiAccessKeyId", "testcowz@yahoo.com");
        request.Headers.Add("apiSecretAccessKey", "cowz1!");
        request.ContentType = "application/json";
        //Request Body
        var myDateTime = DateTime.Now;
        string postData = "{'"cancellationPolicy'": '"SpecificDate'",";
        postData += "'"cancellationEffectiveDate'":'"" + myDateTime.Date + "'",";
        postData += "'"invoiceCollect'": false}";
        ASCIIEncoding encoding = new ASCIIEncoding ();
        byte[] byte1 = encoding.GetBytes (postData);
        request.ContentLength = byte1.Length;
        Stream newStream = request.GetRequestStream();
        newStream.Write(byte1, 0, byte1.Length);

Webservice调用-我认为Json请求体是错误的

答案在这里

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;
using System.Xml;
namespace ExpiredSubscriptionStatus
{
internal class PutWSCall
{
    public string externalBillingSubscriptionNumber;
    private static string _externalBillingSubscriptionURI = ConfigurationManager.AppSettings["externalBillingSubscriptionURI"];
    public void Put()
    {
        try
        {
            _externalBillingSubscriptionURI += externalBillingSubscriptionNumber + "/cancel";
            Uri address = new Uri(_externalBillingSubscriptionURI);
            // Create the web request  
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
            // Set Headers and Content
            request.Method = "PUT";
            request.Headers.Add("apiAccessKeyId", "login");
            request.Headers.Add("apiSecretAccessKey", "password");
            request.ContentType = "application/json";

            //Build the Request Body
            var myDateTime = DateTime.Now;
            ZuoraCancelRequest zuoraRequestClass = new ZuoraCancelRequest();
            zuoraRequestClass.cancellationPolicy = "SpecificDate";
            zuoraRequestClass.cancellationEffectiveDate =  myDateTime.Date.ToString("yyyy-MM-dd");
            zuoraRequestClass.invoiceCollect = false;
            //Serialize the Object and add it to the WS request stream
            MemoryStream stream = new MemoryStream();
            DataContractJsonSerializer ReqSer = new DataContractJsonSerializer(typeof(ZuoraCancelRequest));
            ReqSer.WriteObject(stream, zuoraRequestClass);  //Serialize
            stream.Position = 0;
            StreamReader sr = new StreamReader(stream);
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] bytes = encoding.GetBytes(sr.ReadToEnd());
            request.ContentLength = bytes.Length;
            //Send the WS request stream
            Stream newStream = request.GetRequestStream();
            newStream.Write(bytes, 0, bytes.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream(); //get the stream containing response data sent by the server
            //Deserialize the Json Response
            using (StreamReader reader = new StreamReader(dataStream, Encoding.UTF8))
            {
                ZuoraCancelResponse zuoraResponseClass = new ZuoraCancelResponse();
                MemoryStream memoryStream = new MemoryStream();
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ZuoraCancelResponse));
                ser.WriteObject(memoryStream, zuoraResponseClass);
                memoryStream.Position = 0;
                ZuoraCancelResponse zuoraCancelResponse = (ZuoraCancelResponse)ser.ReadObject(dataStream);
            }
        }
        catch (Exception ex)
        {
            throw ex;  //throw error back up to calling method
        }
    }
}

}