WebAPI获取POST参数
2021-03-18 11:25
标签:margin bytes return 字符串 static content ons nbsp send 接口代码: [HttpPost] public void PostTest() { string content = Request.Content.ReadAsStringAsync().Result; } 请求代码: /// 接口地址 public static string SendRequestPost(string address, string data) { WebRequest request = null; request = WebRequest.Create(address); requestStream = request.GetRequestStream(); response = request.GetResponse(); return responseString; } WebAPI获取POST参数 标签:margin bytes return 字符串 static content ons nbsp send 原文地址:https://www.cnblogs.com/JosephBee/p/12359516.html
/// Json格式字符串参数
WebResponse response = null;
Stream requestStream = null;
StreamReader responseStream = null;
string responseString = null;
string method = "POST";
string contentType = "application/json";
byte[] bytes = Encoding.UTF8.GetBytes(data);
request.ContentType = contentType;
request.Method = method;
request.Timeout = 300000;
request.ContentLength = bytes.Length;
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
responseStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
responseString = responseStream.ReadToEnd();
response.Close();