1、WebApi设置成Post请求
在方法名加特性[HttpPost]或者方法名以Post开头
如下截图:

2、使用(服务端要与客户端对应起来)
【单一字符串方式】:
注意:ContentType = "application/x-www-form-urlencoded";
格式一:

客户端参数格式:如 "=abc123"或者{'':abc123}

        [HttpPost]
public string PostHello([FromBody] string str)
{
return "Hello," + str;
}

格式二:

客户端参数格式: url地址加上Values 如:"http://localhost:21235/api/Products/PostTest?str=abc123"

        public string PostTest(string str)
{
return str;
}

格式三:

客户端参数格式:// 如:"Name=MrBlack"

        public string PostTest2()
{
string name = HttpContext.Current.Request.Form["Name"].ToString();
return name ;
}

【实体类作为参数】(json格式或Dictionary)

注意:request.ContentType = "application/json; charset=utf-8 ";

客户端参数:如 {"Id":"9527","Name":"zhangSan","Category":"A8","Price":"88"}
或者Id=9527&Name=zhangSan&Category=A8&Price=88

格式一:

        public HttpResponseMessage PostProduct([FromBody] Product product)
{
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent("{\"name\":\"hello\"}", Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}

格式二:

        [HttpPost]
public object SaveData(dynamic obj)
{
var strName = Convert.ToString(obj.Name);
return strName;
}

格式三:

        [HttpPost]
public string PostGetProduct(JObject product)
{
return JsonConvert.SerializeObject(product);
}

最好加上几个常见的请求方法:

        public static string HttpPost(string url, string PostData)
{
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Accept = "text/html, application/xhtml+xml, */*";
//request.ContentType = "application/x-www-form-urlencoded ";//根据服务端进行 切换
request.ContentType = "application/json; charset=utf-8 "; byte[] buffer = encoding.GetBytes(PostData);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, , buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return reader.ReadToEnd();
}
}  
public static string HttpPost(string url, IDictionary<string, string> dic)
{
HttpWebRequest request = null;
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
//POST参数拼接
if (!(dic == null || dic.Count == ))
{
StringBuilder buffer = new StringBuilder();
int i = ;
foreach (string key in dic.Keys)
{
if (i > )
{
buffer.AppendFormat("&{0}={1}", key, dic[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, dic[key]);
}
i++;
}
byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, , data.Length);
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return reader.ReadToEnd();
}
}

HttpWebRequest Post请求webapi的更多相关文章

  1. 请求WebApi的几种方式

    目前所了解的请求WebAPI的方式有通过后台访问api 和通过js 直接访问api接口 首先介绍下通过后台访问api的方法,可以使用HttpClient的方式也可以使用WebRequest的方式 1. ...

  2. C# HttpClient请求Webapi帮助类

    引用 Newtonsoft.Json // Post请求 public string PostResponse(string url,string postData,out string status ...

  3. C#使用HttpWebRequest 进行请求,提示 基础连接已经关闭: 发送时发生错误。

    本人今天遇到的错误,C#使用HttpWebRequest 进行请求,提示 基础连接已经关闭: 发送时发生错误. 测试了很久,才发现,是安全协议问题,把安全协议加上就可以了

  4. Fiddler4无法抓取HttpWebRequest本地请求的解决办法

    网上很多解决案例是如下方代码设置代理,但在我的Fiddler4环境下无效,后寻得官方处理方法证实与代理无关. HttpWebRequest request= WebRequest.Create(&qu ...

  5. google插件跨域含用户请求WebApi解决的方案

    问题描述: google插件跨域请求WebApi相关解决方案 1.ajax解决含登录用户信息 $.ajax({ url: url, type: "POST", timeout: 6 ...

  6. jquery.ajax 跨域请求webapi,设置headers

    解决跨域调用服务并设置headers 主要的解决方法需要通过服务器端设置响应头.正确响应options请求,正确设置 JavaScript端需要设置的headers信息 方能实现. 1.第一步 服务端 ...

  7. HttpWebRequest post 请求超时问题

    在使用curl做POST的时候, 当要POST的数据大于1024字节的时候, curl并不会直接就发起POST请求, 而是会分为俩步, 发送一个请求, 包含一个Expect:100-continue, ...

  8. 通过HTTP请求WEBAPI的方式

    平时工作中长期需要用到通过HTTP调用API进行数据获取以及文件上传下载(C#,JAVA...都会用到).这里获取的是包括网页的所有信息.如果单纯需要某些数据内容.可以自己构造函数甄别抠除出来!一般的 ...

  9. C# HttpWebRequest post 请求传参数

    Dictionary<string, string> parameters = new Dictionary<string, string>(); //参数列表 paramet ...

随机推荐

  1. js 联动下拉菜单

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. css animation fade in

    <html> <style> @-webkit-keyframes fadeIn{0%{opacity:0}100%{opacity:1}} @-o-keyframes fad ...

  3. word页眉添加横线与删除横线

    一.删除横线 1.打开已有页眉Word2010文档,并且页眉有横线的,双击页眉 2.选中整个页眉段落,注意:一定要选择段落标记. 3.单击菜单“开始”功能模块. 4.在“段落”中单击边框线下三角按钮. ...

  4. Python的Profilers性能分析器

    关于Python Profilers性能分析器 关于性能分析,python有专门的文档,可查看:http://docs.python.org/library/profile.html?highligh ...

  5. mysql 操作数据库创建,增删改查

    创建数据库 默认字符编码 默认排序CREATE DATABASE IF NOT EXISTS day11 DEFAULT CHARSET utf8 COLLATE utf8_general_ci; / ...

  6. mpdf Could not find image file (http://local.com/xxxxx)

    记录一下昨天和今天遇到的,yii2使用mpdf的时候,图片是使用php方法生成的二维码,所以图片地址为http://local.com/xxxxx,url中携带不同的参数. 但是开启了 $mpdf-& ...

  7. VMware Workstation 12.5.9 Pro虚拟机软件中文版

    更新为 VMware Workstation 12.5.9 pro版.VMware虚拟机软件无疑是windows系统下最强大好用的虚拟机软件.最新的VMware Workstation 12 Pro ...

  8. python—datetime time 模板学习

    写在前面:本人在学习此内容是通过 https://www.cnblogs.com/pycode/p/date.html 文章学习! 时间模块——time python 中时间表示方法有:时间戳_:格式 ...

  9. str 操作方法

    # str 类,字符串 # name ='alex' # 首字母变大写 # test ='alex' # v= test.capitalize() # print(v) # # 大写全部变小写 # t ...

  10. Sean McGinnis

    * Loaded log from Wed Nov 25 22:19:43 2015 * Now talking on #openstack-smaug* [smcginnis] (~smcginni ...