c# webapi POST 参数解决方法

59 篇文章 0 订阅

http://blog.csdn.net/wyqlxy/article/details/49303345

HttpWebRequest POST请求webapi:如果参数是简单类型,比如字符串(注意,拼接的字符串要HttpUtility.UrlEncode才行,否则服务端会丢失特殊字符&后面的数据)

要点:如下代码统一设置为:ContentType = "application/x-www-form-urlencoded";

服务端代码1:URL格式为 POSTapi/Values

public string Post([FromBody] string value)

则客户端Post的数据:拼接的字符串必须以 =开头,否则服务端无法取得value。例如:=rfwreewr2332322232 或者 {'':value}

服务端代码2:URL格式为 POST api/Values?value={value}

public string Post(string value)

则客户端Post的数据:需要url里拼接出KeyValue这样的数据

服务端代码3:URL格式为 POST api/Values

public string Post()

则客户端Post的数据:无要求。例如:key=rfwreewr2332322232。

服务端:可以用HttpContext.Current.Request.InputStream或者HttpContext.Current.Request.Form[0]都可以获取


如果post的参数类型比较复杂,则需要自定义类

要点:如下代码统一设置为:ContentType = "application/json";

服务端代码1:URL格式为 POST api/Values

public string Post([FromBody] Model value)或者 public string Post(Model value)

则客户端Post的数据:{\"id\":\"test1\",\"name\":\"value\"}。服务端会自动映射到对象。

提交代码如下:

[csharp]  view plain  copy
  1. HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create("http://localhost:37831/api/Values");  
  2. wReq.Method = "Post";  
  3. //wReq.ContentType = "application/json";  
  4. //wReq.ContentType = "application/x-www-form-urlencoded";  
  5. wReq.ContentType = "application/json";  
  6.   
  7. //byte[] data = Encoding.Default.GetBytes(HttpUtility.UrlEncode("key=rfwreewr2332322232&261=3&261=4"));  
  8. byte[] data = Encoding.Default.GetBytes("{\"id\":\"test1\",\"name\":\"value\"}");  
  9. wReq.ContentLength = data.Length;  
  10. Stream reqStream = wReq.GetRequestStream();  
  11. reqStream.Write(data, 0, data.Length);  
  12. reqStream.Close();  
  13. using (StreamReader sr = new StreamReader(wReq.GetResponse().GetResponseStream()))  
  14. {  
  15.     string result = sr.ReadToEnd();  
  16.   
  17.      
  18. }  
服务段代码如下:

[csharp]  view plain  copy
  1. // POST api/values  
  2. //public string Post()  
  3. //{  
  4. //    FileInfo fi = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log.txt");  
  5. //    StreamWriter sw = fi.CreateText();  
  6. //    StreamReader sr = new StreamReader(HttpContext.Current.Request.InputStream);  
  7. //    sw.WriteLine("1:" + sr.ReadToEnd()+"--"+ HttpContext.Current.Request.Form[0]);  
  8. //    sw.Flush();  
  9. //    sw.Close();  
  10. //    return "{\"test\":\"1\"}";  
  11. //}  
  12.   
  13. // POST api/values  
  14. public HttpResponseMessage PostStudent(Student student)  
  15. {  
  16.     FileInfo fi = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log.txt");  
  17.     StreamWriter sw = fi.AppendText();  
  18.   
  19.     sw.WriteLine("2:" + student.id);  
  20.     sw.Flush();  
  21.     sw.Close();  
  22.     HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent("{\"test\":\"2\"}", Encoding.GetEncoding("UTF-8"), "application/json") };  
  23.     return result;  
  24. }  


这篇文章里有的方法也不错:http://www.cnblogs.com/rohelm/p/3207430.html



0
0
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值