C# Android 休息

C# Android REST

我使用 Visual studio 和 Xamarin,c#。

我已经使用 Slim、PHP 和 MySql 完成了 REST 服务。它工作正常(chrome ARC 很棒)。

我希望能够从应用程序中使用 POST、GET、PUT 和 DELETE。网上搜了一大堆,没找到C#的(总是java,我确定我得学好!),或者如果是C#的就不适合了移动应用程序(例如使用 System.Net.Http)。

(我的rest是用application/x-www-form-urlencoded作为Header的Content-Type,不知道有没有变化)

我不知道,任何建议都会受到赞赏。

(抱歉,如果它看起来像转贴,但实际上,我搜索了 3 个小时没有找到任何方法)

您是否正在寻找可以让您在应用程序中使用 REST 服务的东西?

检查此 blog post on network services for Xamarin. It talks about using Refit which is a really nice library here is its github

What's Refit? Refit (along with http://json2csharp.com) allow you to really quickly create client libraries for Web APIs, by defining their contract in an Interface, and letting Refit do the grunt work of implementing the API for you. Here's an example:

public interface IGitHubService  
{
  [Get("/users/{user}/repos")]
  Task<List<Repo>> ListRepos(string user);
}

如你所见here

它也支持application/x-www-form-urlencoded

Form posts

For APIs that take form posts (i.e. serialized as application/x-www-form-urlencoded), initialize the Body attribute with BodySerializationMethod.UrlEncoded.

The parameter can be an IDictionary:

public interface IMeasurementProtocolApi
{
    [Post("/collect")]
    Task Collect([Body(BodySerializationMethod.UrlEncoded)] Dictionary<string, object> data);
}

var data = new Dictionary<string, object> {
    {"v", 1}, 
    {"tid", "UA-1234-5"}, 
    {"cid", new Guid("d1e9ea6b-2e8b-4699-93e0-0bcbd26c206c")}, 
    {"t", "event"},
};

// Serialized as: v=1&tid=UA-1234-5&cid=d1e9ea6b-2e8b-4699-93e0-0bcbd26c206c&t=event
await api.Collect(data);