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. vue项目中禁止移动端双击放大,双手拉大放大的方法

    在vue打包后生成的dist目录文件下下面有index.html 打开里面 把原来的这个 <meta name=viewport content="width=device-width ...

  2. Hanlp(汉语言处理包)配置、使用、官方文档

    配置使用教程:https://github.com/hankcs/HanLP Hanlp官方文档:http://www.hankcs.com/nlp/hanlp.html 参考API:http://h ...

  3. gradle 使用本地maven 仓库 和 提交代码到maven

    /* * This build file was generated by the Gradle 'init' task. * * This generated file contains a sam ...

  4. Jenkins自动化CI CD流水线之6--构建邮件状态通知

    一. 前提 前提: 服务器开启邮箱服务: 二. 基础配置 需要安装一个插件: 插件: Email Extension Plugin 进行配置: 系统管理->系统设置-> 相关配置如下图: ...

  5. 测试用例逐步演进-xmind2excel(Python版)测试用例逐步演进-xmind2excel(Python版)

    最近,我在做项目的时候,经常被问到一个问题:如何做测试评审会更有效呢? 只要做过测试用例评审,特别是比较复杂的测试用例评审的时候,很多测试同学都会苦恼于如何能更有效的向大家说出自己的测试设计思路. 当 ...

  6. py---------面向对象基础篇

    引子 你现在是一家游戏公司的开发人员,现在你需要开发一款叫做<人猫大战>的小游戏,你就思考呀,人猫大战,那至少需要两个角色,一个是人,一个是猫,且人和猫有不同的技能,比如人拿棍打狗,狗可以 ...

  7. Linux Vi/Vim 在插入模式下键盘右边数字键输入异常

    问题:Linux在 Vi/Vim 在编辑文件时遇到一个问题,当我在 Insert 模式下进行修改文件内容的时候,用到了键盘(104键,右边带数字键那种)进行数字输入,当我输入数字 “5” 时,插入的数 ...

  8. hive - load CSV file NULL value 加载csv文件出现结果全是空值

    这个问题的根源是,创建表的时候没有指定列分隔符还有行分隔符. 因此修改建表语句 问题依然重现,此问题苦恼了一个下午,有一次用describe tablename 发现了问题所在,原来是一直没有删除ta ...

  9. 001 Two Sum 两个数的和为目标数字

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  10. java多线程(二)

    线程的阻塞状态: 参考java多线程(一)多线程的生命周期图解,多线程的五种状态.     1.1 join(),如果在A线程体里面执行了B线程的join()方法,那么A线程阻塞,直到B线程生命周期结 ...