.net c#调用Web Api Post请求传输数据,用.net8一直传不了自定义的json格式数据,后面找到用实体传递Api那边用一样字段的实体接收才能正常传输数据。记录一下
var mails = new
{Name = "tt",Hobby = "test"
};
string json = JsonConvert.SerializeObject(mails );
string url = "http://192.168.8.107:8002/api/OriginalData/UploadMails";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = false;
request.Method = "POST";
request.ContentType = "application/json";
try
{//GET方法没有写入流这一部分using (StreamWriter writer = new StreamWriter(request.GetRequestStream())){writer.Write(json);}HttpWebResponse response = null;try{response = (HttpWebResponse)request.GetResponse();}catch (WebException e){response = (HttpWebResponse)e.Response;}
}
catch (Exception e)
{throw e;}//web api 接收实体字段和传递的要一样
[HttpPost]
public async Task<int> UploadMails([FromBody] mails mails){}