WebAPI获取POST参数

2021-03-18 11:25

阅读:638

标签:margin   bytes   return   字符串   static   content   ons   nbsp   send   

接口代码:

[HttpPost]

public void PostTest()

{

  string content = Request.Content.ReadAsStringAsync().Result;

}

 

请求代码:

/// 接口地址
/// Json格式字符串参数

public static string SendRequestPost(string address, string data)

{

WebRequest request = null;
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 = WebRequest.Create(address);
request.ContentType = contentType;
request.Method = method;
request.Timeout = 300000;
request.ContentLength = bytes.Length;

requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();

response = request.GetResponse();
responseStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
responseString = responseStream.ReadToEnd();
response.Close();

return responseString;

}

WebAPI获取POST参数

标签:margin   bytes   return   字符串   static   content   ons   nbsp   send   

原文地址:https://www.cnblogs.com/JosephBee/p/12359516.html


评论


亲,登录后才可以留言!