[英]Post JSON data to web API using object in C#
我能够编写代码以从Web API执行GET操作。 但是,我无法发布。 我认为问题出在JSON对象上。 如果参数是通过URL发送的,则可以发布消息,但是如果它是JSON对象,则无法发送消息。 例如:POST要求我通过URL和ReferenceString作为JSON对象发送ModelID,CustomerID。
数据发布
型号ID = 3345
客户编号= 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);
}
POST请求
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();
}
创建一个DTO类作为参考ID
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.