WebAPI获取POST参数
Posted JosephBee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebAPI获取POST参数相关的知识,希望对你有一定的参考价值。
接口代码:
[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参数的主要内容,如果未能解决你的问题,请参考以下文章
FormBody and FormUri attribute in webapi
一些简单的Post方式WebApi接收参数和传递参数的方法及总结