HttpWebRequest Post请求webapi
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的更多相关文章
- 请求WebApi的几种方式
目前所了解的请求WebAPI的方式有通过后台访问api 和通过js 直接访问api接口 首先介绍下通过后台访问api的方法,可以使用HttpClient的方式也可以使用WebRequest的方式 1. ...
- C# HttpClient请求Webapi帮助类
引用 Newtonsoft.Json // Post请求 public string PostResponse(string url,string postData,out string status ...
- C#使用HttpWebRequest 进行请求,提示 基础连接已经关闭: 发送时发生错误。
本人今天遇到的错误,C#使用HttpWebRequest 进行请求,提示 基础连接已经关闭: 发送时发生错误. 测试了很久,才发现,是安全协议问题,把安全协议加上就可以了
- Fiddler4无法抓取HttpWebRequest本地请求的解决办法
网上很多解决案例是如下方代码设置代理,但在我的Fiddler4环境下无效,后寻得官方处理方法证实与代理无关. HttpWebRequest request= WebRequest.Create(&qu ...
- google插件跨域含用户请求WebApi解决的方案
问题描述: google插件跨域请求WebApi相关解决方案 1.ajax解决含登录用户信息 $.ajax({ url: url, type: "POST", timeout: 6 ...
- jquery.ajax 跨域请求webapi,设置headers
解决跨域调用服务并设置headers 主要的解决方法需要通过服务器端设置响应头.正确响应options请求,正确设置 JavaScript端需要设置的headers信息 方能实现. 1.第一步 服务端 ...
- HttpWebRequest post 请求超时问题
在使用curl做POST的时候, 当要POST的数据大于1024字节的时候, curl并不会直接就发起POST请求, 而是会分为俩步, 发送一个请求, 包含一个Expect:100-continue, ...
- 通过HTTP请求WEBAPI的方式
平时工作中长期需要用到通过HTTP调用API进行数据获取以及文件上传下载(C#,JAVA...都会用到).这里获取的是包括网页的所有信息.如果单纯需要某些数据内容.可以自己构造函数甄别抠除出来!一般的 ...
- C# HttpWebRequest post 请求传参数
Dictionary<string, string> parameters = new Dictionary<string, string>(); //参数列表 paramet ...
随机推荐
- 雷林鹏分享:jQuery EasyUI 数据网格 - 添加查询功能
jQuery EasyUI 数据网格 - 添加查询功能 本实例演示如何从数据库得到数据,并将它们显示在数据网格(datagrid)中.然后演示如何根据用户输入的搜索关键词搜寻显示结果. 创建数据网格( ...
- apache 压缩 gzip
配置 编辑httpd.conf文件 去掉 #LoadModule headers_module modules/mod_headers.so 前面的注释# 去掉 #LoadModule deflate ...
- UVA 10806
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> us ...
- 自定义Razor 标签
1.首先需要一个abstract class WebViewPage<T> ,继承系统的 System.Web.Mvc.WebViewPage<TModel> 再定义一个Web ...
- 在Javascript中 声明时用"var"与不用"var"的区别
http://www.cnblogs.com/juejiangWalter/p/5725220.html var num = 0;function start() { num = 3;} 只要一 ...
- vue(1)安装
1.安装node.js(https://nodejs.org/en/),我安装的是 v10.15.1 1).在nodejs安装路径下,新建node_global和node_cache两个文件夹 2). ...
- vue 的watch用法
参考转自https://www.imooc.com/article/details/id/28187 类型:{ [key: string]: string | Function | Object | ...
- mysql 死锁解决办法
查询表的时候,发现一圈圈转啊转,就是不出来数据,猜测表被锁住 解决办法 : mysql> show processlist ; mysql> kill 4; 说明 : 4为 i ...
- dom4j使用
http://www.cnblogs.com/zfc2201/archive/2011/08/16/2141441.html http://www.blogjava.net/i369/articles ...
- JMeter远程分布式联机性能测试
测试环境 apache-jmeter-2.13 Java JDK版本:1.8 1. JMeter分布式测试简介 当一个JMeter客户端因网络限制等因素,无法模拟足够的用户对服务器施压时,需要用到J ...