如何使用RestSharp执行get请求?

时间:2011-08-09 16:33:24

标签: windows-phone-7 restsharp

我无法确定如何在Windows Phone 7上使用RestSharp发出GET请求。所有示例都显示发出POST请求,但我只需要GET。我该怎么做?

2 个答案:

答案 0 :(得分: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
});

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

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

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

答案 1 :(得分:2)

您要找的是here

下方(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);
        }
    });
}