本文主要是介绍.net Web Api Post请求传递数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
.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){}
这篇关于.net Web Api Post请求传递数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!