在此处发出HttpClient请求:
private static void PostPersonInfoAsync1()
{
Person person = new Person
{
Id = "999",
CompanyName = "ABC",
Department = "CDE",
DisplayName = "XYZ",
JobTitle = "manager",
OfficeLocation = "zzz"
};
HttpClient client = new HttpClient();
var myContent = JsonConvert.SerializeObject(person);
Console.WriteLine(myContent);
//var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
var stringContent = new StringContent(myContent);
stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var result = client.PostAsync("http://localhost:52810/Person/AddPerson", stringContent).Result;
}
这是端点命中的位置:
[HttpPost]
public JsonResult AddPerson(Person person)
{
System.Diagnostics.Debug.WriteLine("ADD PERSON ::::" + person.ToString());
string jsonIsSuccess = "";
//System.Diagnostics.Debug.WriteLine(jsonPerson.ToString() + " is being added");
//Person person = m_jsonConverter.ConvertToObject(jsonPerson.ToString());
if (!(person.Id == null || person.JobTitle == null || person.OfficeLocation == null ||
person.Department == null || person.DisplayName == null || person.CompanyName == null))
{
if (m_personRepository.AddPerson(person))
{
jsonIsSuccess = "{" +
"\"status\":\"success\"" +
"}";
}
}
else
{
jsonIsSuccess = "{" +
"\"status\":\"failed\"" +
"}";
}
return new JsonResult(jsonIsSuccess);
}
在下一行的所有参数中获取空值
System.Diagnostics.Debug.WriteLine("ADD PERSON ::::" + person.ToString());
当我使用Postman检查时,一切都很好。但是当我使用HttpClient创建控制台应用程序时,它无法正常工作。