如何使用RestSharp执行get请求?

Chr*_*her 6 windows-phone-7 restsharp

我在Windows Phone 7上使用RestSharp查明如何发出GET请求时遇到了麻烦.所有示例都显示发出POST请求,但我只需要GET.我该怎么做呢?

Joh*_*han 16

GET是RestSharp使用的默认方法,因此如果您不指定方法,它将使用GET:

var client = new RestClient("http://example.com");
var request = new RestRequest("api");

client.ExecuteAsync(request, response => {
    // do something with the response
});
Run Code Online (Sandbox Code Playgroud)

此代码将发出GET请求http://example.com/api.如果您需要添加URL参数,可以执行以下操作:

var client = new RestClient("http://example.com");
var request = new RestRequest("api");    
request.AddParameter("foo", "bar");
Run Code Online (Sandbox Code Playgroud)

这转化为 http://example.com/api?foo=bar


Den*_*sky 2

您要找的东西就在这里

涵盖您的场景的代码片段如下(request.Method应设置为Method.GET):

public void GetLabelFeed(string label, Action<Model.Feed> success, Action<string> failure)
{
    string resource = "reader/api/0/stream/contents/user/-/label/" + label;

    var request = GetBaseRequest();
    request.Resource = resource;
    request.Method = Method.GET;
    request.AddParameter("n", 20); //number to return

    _client.ExecuteAsync<Model.Feed>(request, (response) =>
    {
        if (response.ResponseStatus == ResponseStatus.Error)
        {
            failure(response.ErrorMessage);
        }
        else
        {
            success(response.Data);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)