使用NuGet包安装Microsoft ASP.NET Web API 2.1 Client Libraries,

调用方式代码如下:

        HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:2471/");//基地址
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("api/values").Result;
if (response.IsSuccessStatusCode)
{
var products = response.Content.ReadAsAsync<IEnumerable<ProductInfo>>().Result;
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
} //HTTP Post请求
ProductInfo pro = new ProductInfo();
pro.dateTime1 = DateTime.Now;
pro.Int = ;
response = client.PutAsJsonAsync<ProductInfo>("api/Values", pro).Result;
if (response.IsSuccessStatusCode)
{ }
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
} response = client.DeleteAsync("api/values/5").Result;
if (response.IsSuccessStatusCode)
{ }
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}

values控制器代码如下:

 public IEnumerable<ProductInfo> GetList()
{
//string s = this.Request.Headers.GetValues("YXLCookie").FirstOrDefault(); ProductInfo p = new ProductInfo();
p.Int = ;
p.string1 = "";
p.dateTime2 = DateTime.Now; ProductItem pi = new ProductItem() { Int = , string1 = "" };
p.productItem = pi; ProductInfo p2 = new ProductInfo();
p2.Int = ;
p2.string1 = "";
p2.dateTime2 = DateTime.Now; ProductItem pi2 = new ProductItem() { Int = , string1 = "", dateTime1 = DateTime.Now };
p2.productItem = pi2; List<ProductInfo> list = new List<ProductInfo>();
list.Add(p);
list.Add(p2); return list;
} // POST api/values
public void PostProduct([FromBody]ProductInfo pro)
{ }
// DELETE api/values/5
public void Delete(int id)
{
}

使用JsonConvert要安装Json.NET,使用NuGet安装Json.NET如果我们新增json格式的数据,需要将json格式转换为object对象,代码如下:

 HttpClient client = httpClient;
client.BaseAddress = new Uri("https://api.weixin.qq.com");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json")); object s1 = JsonConvert.DeserializeObject<object>(@"{
'button':[
{
'type':'click',
'name':'协运跟踪',
'key':'V1001_TODAY_MUSIC'
},
{
'type':'click',
'name':'金牌航线',
'key':'V1001_TODAY_SINGER'
},
{
'name':'菜单测试',
'sub_button':[
{
'type':'view',
'name':'运价',
'url':'http://weixin.51xieyun.com/price/index'
},
{
'type':'view',
'name':'首页',
'url':'http://weixin.51xieyun.com'
},
{
'type':'click',
'name':'赞一下我们',
'key':'V1001_GOOD'
}]
}]
}
");
HttpResponseMessage response = client.PostAsJsonAsync<object>("cgi-bin/menu/create?access_token=dPm9S5I6bMKp3QHhcxxqyJhuaNH4h20lJaIeNi-gXVbkpQCsUdyvOLbfQCPmDFWMIS0LowOXSVfu1iD47VC9nolu29-rRJ0oWUhIX0WRDYKPOIZtf4onsSvFePdt1iX9jba5rmhR769OG7cye--V4g", s1).Result;
if (response.IsSuccessStatusCode)
{
var products = response.Content.ReadAsAsync<object>().Result;
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}

使用HttpClient操作ASP.NET Web API 2.1增删改查的更多相关文章

  1. 使用HttpClient对ASP.NET Web API服务实现增删改查

    本篇体验使用HttpClient对ASP.NET Web API服务实现增删改查. 创建ASP.NET Web API项目 新建项目,选择"ASP.NET MVC 4 Web应用程序&quo ...

  2. 前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查

    AngularJS中的$resource服务相比$http服务更适合与RESTful服务进行交互.本篇后端使用ASP.NET Web API, 前端使用$resource,实现增删改查. 本系列包括: ...

  3. 关于操作 ASP.NET Web API的实例

    WCF的野心造成了它的庞大复杂,HTTP的单纯造就了它的简单优美.为了实现分布式Web应用,我们不得不将两者凑合在一起 —— WCF服务以HTTP绑定宿主于IIS. 于是有了让人晕头转向的配置.让人郁 ...

  4. 使用HttpClient消费ASP.NET Web API服务

    本篇体验使用HttpClient消费ASP.NET Web API服务,例子比较简单. 依次点击"文件","新建","项目". 选择&quo ...

  5. ASP.NET从零开始学习EF的增删改查

           ASP.NET从零开始学习EF的增删改查           最近辞职了,但是离真正的离职还有一段时间,趁着这段空档期,总想着写些东西,想来想去,也不是很明确到底想写个啥,但是闲着也是够 ...

  6. 基于gin的golang web开发:mysql增删改查

    Go语言访问mysql数据库需要用到标准库database/sql和mysql的驱动.标准库的Api使用比较繁琐这里再引入另一个库github.com/jmoiron/sqlx. go get git ...

  7. Android(java)学习笔记193:利用谷歌API对数据库增删改查(推荐使用)

    接下来我们通过项目案例来介绍:这个利用谷歌API对数据库增删改查 1.首先项目图: 2.这里的布局文件activity_main.xml: <LinearLayout xmlns:android ...

  8. 通过flask实现web页面简单的增删改查bootstrap美化版

    通过flask实现web页面简单的增删改查bootstrap美化版 项目目录结构 [root@node1 python]# tree -L 2 . ├── animate.css ├── fileut ...

  9. 通过flask实现web页面简单的增删改查

    通过flask实现web页面简单的增删改查 # 1.后台程序falsk_web01.py #coding:utf-8 from flask import Flask,render_template,r ...

随机推荐

  1. 菲涅尔反射(Fresnel Reflection)

    离线渲染中,通常可以用kd,ks,kt(分别代表物体的漫反射系数,镜面反射系数,透射系数)来简单地描述一个物体的基本材质,例如,我们将一个物体设置为:kd=0,ks=0.1,kt=0.9,即代表一束光 ...

  2. WPF 动态布局Grid

    //开启线程加载 Action a = () => { ; ; var path = "../../face_img/"; var files = Directory.Get ...

  3. WPF 任务栏图标闪烁提醒

    using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServi ...

  4. ios7开发学习笔记-包括c oc 和ios介绍

    请查看我的新浪资料分享 http://iask.sina.com.cn/u/2430843520

  5. jquery基础总结

    什么是jQuery?        就是一个JavaScript函数库,开源的.jQuery能做什么        JavaScript是做什么的,jQuery就是做什么的,Jquery是对javas ...

  6. Centos上传下载小工具lrzsz

    http://www.centoscn.com/image-text/install/2013/0819/1374.html

  7. DEDECMS 后台登录空白怎么办 后台无法登陆

    刚安装完dedecms,兴致冲冲的准备进后台,输入完用户名和密码后,页面 中显示一片空白. 立马到网上搜搜,发现大家各抒己见,但是都没有解决问题. 不过,下面的这个方法是可以的.马上记录下来,以备其他 ...

  8. Altera的几个常用的Synthesis attributes

    各厂商综合工具,对HDL综合时都定义了一些综合属性这些属性可指定a declaration,a module item,a statement, or a port connection 不同的综合方 ...

  9. Scheme笔记(1)

    (define pi 3.14159) (define radius 10) (* pi (* radius radius)) (define circumference (* 2 pi radius ...

  10. Python字符串基础操作

    ==============字符串======== >>> s1='www.baidu.com' >>> type(s1) <type 'str'> & ...