我无法确定如何在Windows Phone 7上使用RestSharp发出GET请求。所有示例都显示发出POST请求,但我只需要GET。我该怎么做?
答案 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);
}
});
}