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 ...
随机推荐
- 直播点赞,上升的动画-- CAKeyFrameAnimation
// // ViewController.m // DMHeartFlyAnimation // // Created by Rick on 16/3/9. // Copyright © 20 ...
- C#后端调用WebApi地址
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using Syst ...
- Luogu P1120 小木棍 [数据加强版] 来来来我们一起来剪枝,剪枝,剪枝、、、
好啊...太棒了... dfs(拼到第几根木棍,这根木棍剩余长度,上一根木棍的位置) len是木棍的长度,cnt是木棍的个数 震撼人心的剪枝: 1.枚举长度从最大的木棍开始,直到sum/2,因为之后只 ...
- 计算机学院大学生程序设计竞赛(2015’12)The collector’s puzzle
Problem Description There is a collector who own many valuable jewels. He has a problem about how to ...
- 前端基础------jquer y学习
一. jquery是什么 快速,简洁,轻量级的JavaScript库(JavaScript框架)使用户可以快速的操作HTML document,实现动画效果,并方便的地为网站提供AJAX交互.文档全面 ...
- 工具类_GsonUtils
import java.lang.reflect.Type; import com.google.gson.Gson; /** * Gson工具类 2015-02-01<br/> * 1, ...
- Linux 网络配置及常用服务配置(Redhat 6)
一.网络配置 1. VMWare 提供了三种网络工作模式供用户选择,他们分别是, ①Bridged(桥接模式): 如果网络中能提供多个IP地址,则使用桥接方式 ②NAT(网络地址转换模式): 如果网络 ...
- Wireshark抓包数据:理解与分析
wireshark是一个非常好用的抓包工具,本文根据平时抓包经验,对之前wireshark抓包的一些常见知识点进行了整理. 有不当之处,欢迎指正 1.SYN,FIN会消耗一个序号,单独的ACK不消耗序 ...
- ubuntu14.04&matlab2015b 测试caffe的Matlab接口
Step1: 修改caffe-master中的Makefile.config 提示:可以到文件中直接“ctrl+f”,键入相应大写字母即可查找到相应位置. Step2:编译接口.如果之前编译caffe ...
- shell脚本学习(1)
格式: #!/bin/bash :标志一个shell脚本 #Filename:first_shell.sh :文件名称 #a ...