c# webapi POST 参数解决方法
2016-09-07 15:07
369 查看
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
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create("http://localhost:37831/api/Values");
wReq.Method = "Post";
//wReq.ContentType = "application/json";
//wReq.ContentType = "application/x-www-form-urlencoded";
wReq.ContentType = "application/json";
//byte[] data = Encoding.Default.GetBytes(HttpUtility.UrlEncode("key=rfwreewr2332322232&261=3&261=4"));
byte[] data = Encoding.Default.GetBytes("{\"id\":\"test1\",\"name\":\"value\"}");
wReq.ContentLength = data.Length;
Stream reqStream = wReq.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
using (StreamReader sr = new StreamReader(wReq.GetResponse().GetResponseStream()))
{
string result = sr.ReadToEnd();
}
服务段代码如下:
[csharp] view
plain copy
// POST api/values
//public string Post()
//{
// FileInfo fi = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log.txt");
// StreamWriter sw = fi.CreateText();
// StreamReader sr = new StreamReader(HttpContext.Current.Request.InputStream);
// sw.WriteLine("1:" + sr.ReadToEnd()+"--"+ HttpContext.Current.Request.Form[0]);
// sw.Flush();
// sw.Close();
// return "{\"test\":\"1\"}";
//}
// POST api/values
public HttpResponseMessage PostStudent(Student student)
{
FileInfo fi = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log.txt");
StreamWriter sw = fi.AppendText();
sw.WriteLine("2:" + student.id);
sw.Flush();
sw.Close();
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent("{\"test\":\"2\"}", Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}
这篇文章里有的方法也不错:http://www.cnblogs.com/rohelm/p/3207430.html
顶0
踩0
上一篇C#
web api返回类型设置为json的两种方法
下一篇浏览器HTTP缓存原理分析
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
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create("http://localhost:37831/api/Values");
wReq.Method = "Post";
//wReq.ContentType = "application/json";
//wReq.ContentType = "application/x-www-form-urlencoded";
wReq.ContentType = "application/json";
//byte[] data = Encoding.Default.GetBytes(HttpUtility.UrlEncode("key=rfwreewr2332322232&261=3&261=4"));
byte[] data = Encoding.Default.GetBytes("{\"id\":\"test1\",\"name\":\"value\"}");
wReq.ContentLength = data.Length;
Stream reqStream = wReq.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
using (StreamReader sr = new StreamReader(wReq.GetResponse().GetResponseStream()))
{
string result = sr.ReadToEnd();
}
服务段代码如下:
[csharp] view
plain copy
// POST api/values
//public string Post()
//{
// FileInfo fi = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log.txt");
// StreamWriter sw = fi.CreateText();
// StreamReader sr = new StreamReader(HttpContext.Current.Request.InputStream);
// sw.WriteLine("1:" + sr.ReadToEnd()+"--"+ HttpContext.Current.Request.Form[0]);
// sw.Flush();
// sw.Close();
// return "{\"test\":\"1\"}";
//}
// POST api/values
public HttpResponseMessage PostStudent(Student student)
{
FileInfo fi = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log.txt");
StreamWriter sw = fi.AppendText();
sw.WriteLine("2:" + student.id);
sw.Flush();
sw.Close();
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent("{\"test\":\"2\"}", Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}
这篇文章里有的方法也不错:http://www.cnblogs.com/rohelm/p/3207430.html
顶0
踩0
上一篇C#
web api返回类型设置为json的两种方法
下一篇浏览器HTTP缓存原理分析
相关文章推荐
- c# webapi POST 参数解决方法
- c# webapi POST 参数解决方法
- C# webconfig配置解决post 传递中文参数乱码问题
- C# WebApi Xml序列化问题解决方法:“ObjectContent`1”类型未能序列化内容类型“application/xml;charset=utf-8"的响应正文。...
- 用HttpWebRequest做POST请求时返回Http 417 错误解决方法
- POST方法传递参数,返回过期解决方法
- Delphi 调用C#编写的WebService 参数为Null解决方法
- jsp 页面之间 get / post 方法 传递参数内容 乱码 解决之道
- BASE64-客户端(js)加码-服务器端(c#)解码,URL传递“汉字参数”解决方法。
- Delphi 调用C#编写的WebService 参数为Null解决方法
- 山寨AjaxPro,解决分布式问题,实现主流接口,保留原版的API方法,唯一的不同是稍微修改下webconfig
- WebClient.UploadValues Post中文乱码的解决方法
- BASE64-客户端(js)加码-服务器端(c#)解码,URL传递“汉字参数”解决方法
- C#WinForm页面传值解决方法(仿web中Session)
- Delphi 调用C#编写的WebService 参数为Null解决方法
- C# post数据时 出现如下错误: System.Net.WebException: 远程服务器返回错误: (417) Expectation Failed 的解决办法
- 用HttpWebRequest做POST请求时返回Http 417 错误解决方法
- [转]C# post数据时 出现如下错误: System.Net.WebException: 远程服务器返回错误: (417) Expectation Failed 的解决办法
- My Web Access Code in C#(Basic Post&Get method) / C#访问Web代码(基本Post和Get方法)
- Delphi (c++builder) 调用C#编写的WebService 参数为Null解决方法