使用 C# 中的对象将 JSON 数据发布到 Web API

sho*_*ave 3 .net c# json web-services http

我能够编写代码来从 Web API 执行 GET 操作。但是,我无法发布。我认为问题出在 JSON 对象上。如果参数是通过 URL 发送的,我可以 POST,但如果它是 JSON 对象,我就无法这样做。例如:POST 要求我通过 URL 发送 ModelID、CustomerID,并将 ReferenceString 作为 JSON 对象发送。

发送数据

型号 ID = 3345

客户 ID =1V34858493

ReferenceID 是一个 JSON 字符串[]

[{“ReferenceId”:“a123”}]

主要的

 static void Main(string[] args) 
    {
       // JavaScriptSerializer serializer = new JavaScriptSerializer();

        string patientServiceResponse = PostRequest( string.Format("https://url.com/api/{0}/{1}/taskOrders", 3345, "1V34858493"));

        Debug.Write(patientServiceResponse);
    }
Run Code Online (Sandbox Code Playgroud)

邮寄请求

private static string PostRequest(string url)
    {
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "application/json; charset=utf-8";
        httpWebRequest.Method = "POST";
        httpWebRequest.Accept = "application/json; charset=utf-8";
        string sContentType = "application/json";

        JObject oJsonObject = new JObject();

        oJsonObject.Add("ReferenceId", "a123");

        HttpClient oHttpClient = new HttpClient();
        var oTaskPostAsync = oHttpClient.PostAsync(url, new StringContent(oJsonObject.ToString(), Encoding.UTF8, sContentType));

        //return 
    }
Run Code Online (Sandbox Code Playgroud)

你能纠正我哪里错了吗?

sho*_*ave 5

感谢梅森!我编写了使用 .NET 将数据 POST 到 Web API 的代码HttpWebRequest

主要的

static void Main(string[] args) 
{
    string patientServiceResponse = PostRequest( string.Format("https://url.com/api/{0}/{1}/taskOrders", 3345, "1V34858493"));

    Debug.Write(patientServiceResponse);
}
Run Code Online (Sandbox Code Playgroud)

邮政

private static string PostRequest(string url)
    {
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "application/json; charset=utf-8";
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "[  { \"ReferenceId\": \"a123\"  } ]";
            Debug.Write(json);
            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }
        try
        {
            using (var response = httpWebRequest.GetResponse() as HttpWebResponse)
            {
                if (httpWebRequest.HaveResponse && response != null)
                {
                    using (var reader = new StreamReader(response.GetResponseStream()))
                    {
                        result = reader.ReadToEnd();
                    }
                }
            }
        }
        catch (WebException e)
        {
            if (e.Response != null)
            {
                using (var errorResponse = (HttpWebResponse)e.Response)
                {
                    using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                    {
                        string error = reader.ReadToEnd();
                        result = error;
                    }
                }

            }
        }

        return result;

    }
Run Code Online (Sandbox Code Playgroud)