码迷,mamicode.com
首页 > Windows程序 > 详细

WebAPI获取POST参数

时间:2020-02-25 00:15:17      阅读:256      评论:0      收藏:0      [点我收藏+]

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

接口代码:

[HttpPost]

public void PostTest()

{

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

}

 

请求代码:

/// <param name="address">接口地址</param>
/// <param name="data">Json格式字符串参数</param>

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!