WebAPi

来源:互联网 发布:java web即时通讯源码 编辑:程序博客网 时间:2024/04/26 07:08
RestSharp.WindowsPhone调用Rest服务
今天发现RestSharp.WindowsPhone调用Rest服务非常简单,而且功能非常强大,对于不支持的动词也省去不少麻烦,功能如下:
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 client = new RestClient("URL地址");
var rest 
= new RestRequest(Method.GET);

client.ExecuteAsync(rest, (response) 
=>
{
     
string re = response.Content;//返回的结果
});
复制代码
POST调用方法:
复制代码
var client = new RestClient("URL地址");
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();
https://github.com/restsharp/RestSharp


    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();

0 0
原创粉丝点击