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



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

要开机自检的数据

型号 ID = 3345

客户 ID =1V34858493

引用 ID 是一个 JSON 字符串[]

[ { "参考 ID": "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);
}

开机自检请求

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 
}

你能纠正我哪里出错吗?

感谢梅森!我编写了代码以使用HttpWebRequest将数据发布到 Web API。

主要

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

发布

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;
}

POST 函数将如下所示。

[HttpPost]
[Route("{modelId}/{customerId}")]
public IHttpActionResult Add(string modelId, string customerId, REFDto referenceId)
{
return Ok();
}

为引用 ID 创建 DTO 类

public class REFDto
{
public string referenceId { get; set; }
}

API 客户端调用:

using (var client = new HttpClient())
{
var modelId = "3345";
var customerId = "1V34858493";
var url = $"https://url.com/api/{modelId}/{customerId}/taskOrders";
JObject oJsonObject = new JObject();
oJsonObject.Add("referenceId", "ref123");
var response = await client.PostAsync(url, new StringContent(oJsonObject.ToString(), Encoding.UTF8, "application/json"));
Console.WriteLine(response);
}

相关内容

  • 没有找到相关文章

最新更新