WebAPi
来源:互联网 发布:java web即时通讯源码 编辑:程序博客网 时间:2024/04/26 07:08
1.支持JSON.NET
2.支持.net4.0
3.支持Silverlight4.0
4.支持Windows Phone 7.0,7.1(mango)
5.支持XML,JSON序列化
6.支持mono
7.支持PUT,DELETE,GET,POST等动词
有兴趣的朋友可以试试最新版本为101.3.0.0,点击下载
GET调用方法:
var rest = new RestRequest(Method.GET);
client.ExecuteAsync(rest, (response) =>
{
string re = response.Content;//返回的结果
});
var rest = new RestRequest(Method.POST);
rest.RequestFormat = DataFormat.Json; //请求传递参数为JSON
rest.AddHeader("Content-Type", "application/json"); //设置HTTP头
rest.AddBody(account);//account实体类的一个对象
client.ExecuteAsync(rest, (response) =>
{
string re = response.Content; //返回的结果
});
Features
- Supports .NET 3.5+, Silverlight 4, Windows Phone 8, Mono, MonoTouch, Mono for Android
- Easy installation using NuGet for most .NET flavors
- Supports strong naming using NuGet for most .NET flavors
- Automatic XML and JSON deserialization
- Supports custom serialization and deserialization via ISerializer and IDeserializer
- Fuzzy element name matching ('product_id' in XML/JSON will match C# property named 'ProductId')
- Automatic detection of type of content returned
- GET, POST, PUT, HEAD, OPTIONS, DELETE supported
- Other non-standard HTTP methods also supported
- oAuth 1, oAuth 2, Basic, NTLM and Parameter-based Authenticators included
- Supports custom authentication schemes via IAuthenticator
- Multi-part form/file uploads
- T4 Helper to generate C# classes from an XML document
var client = new RestClient("http://example.com");// client.Authenticator = new HttpBasicAuthenticator(username, password);var request = new RestRequest("resource/{id}", Method.POST);request.AddParameter("name", "value"); // adds to POST or URL querystring based on Methodrequest.AddUrlSegment("id", "123"); // replaces matching token in request.Resource// add parameters for all properties on an objectrequest.AddObject(object);// or just whitelisted propertiesrequest.AddObject(object, "PersonId", "Name", ...);// easily add HTTP Headersrequest.AddHeader("header", "value");// add files to upload (works with compatible verbs)request.AddFile("file", path);// execute the requestIRestResponse response = client.Execute(request);var content = response.Content; // raw content as string// or automatically deserialize result// return content type is sniffed but can be explicitly set via RestClient.AddHandler();IRestResponse<Person> response2 = client.Execute<Person>(request);var name = response2.Data.Name;// or download and save file to diskclient.DownloadData(request).SaveAs(path);// easy async supportclient.ExecuteAsync(request, response => { Console.WriteLine(response.Content);});// async with deserializationvar asyncHandle = client.ExecuteAsync<Person>(request, response => { Console.WriteLine(response.Data.Name);});// abort the request on demandasyncHandle.Abort();
var client = new RestClient("http://localhost:809/api/values/setuser");
var request = new RestRequest(Method.POST);
//request.AddUrlSegment("id", "123");
request.RequestFormat = DataFormat.Json;
request.AddBody(new { Id = 2 });
request.AddHeader("Content-Type", "application/json"); //设置HTTP头
var result = client.Execute<dynamic>(request);
var id = result.Data.Id;
//result.Content;
////Console.WriteLine("132");
////var a = GetResponseString("http://localhost:809/api/values", "1212");
//var res = GetResponseString("1", "http://localhost:809/api/values");
//Console.WriteLine(res);
Console.ReadKey();
- WebAPI
- webApi
- webapi
- WebAPi
- webAPI
- webapi
- WebAPI
- webapi
- WebApi
- WebAPI
- WebApi
- WebApi
- MVC4 WebAPI
- MVC4 WebAPI
- 调用webapi
- NodeJs WebApi
- WebAPI请求
- Geolocation WebAPI
- hibernate HQL语句
- PostgreSQL对接SequoiaDB
- MySQL查询和修改auto_increment
- C语言中int到float的强制类型转换(转)
- Android性能优化典范
- WebAPi
- SQL Server中 ldf 文件过大的解决方法
- unity3d读取plist或xml文件
- 使用 HttpClient 和 HtmlParser 实现简易爬虫
- IOS学习之——表视图1 概述
- 计算机中的字,字长,位,字节的概念
- jQuery获取混合radio、checkbox、text值
- 显示SAP里所有的ICON(图标)的程序
- iOS8开发~UI布局(二)storyboard中autolayout和size class的使用详解