2012-03-01 46 views
2

所以我试着去建立RestSharp根据文档 http://momentapp.com/docsRestSharp请求

这里是我的代码使用瞬间任务调度。

public class MomentApi : ITaskScheduler 
    { 
     const string BaseUrl = "https://momentapp.com";  

     private RestResponse Execute(RestRequest request) 
    { 
     var client = new RestClient(); 
     client.BaseUrl = BaseUrl; 
     request.AddParameter("apikey", "MYAPIKEYHERE", ParameterType.UrlSegment); // used on every request 
     var response = client.Execute(request); 
     return response; 
    } 

    public HttpStatusCode ScheduleTask(DateTime date, Uri url, string httpMethod, Uri callback = null) 
    { 
     var request = new RestRequest(Method.POST); 
     request.Resource = "jobs.json"; 
     request.AddParameter("job[uri]", "http://develop.myapp.com/Something"); 
     request.AddParameter("job[at]", "2012-06-31T18:36:21"); 
     request.AddParameter("job[method]", "GET"); 
     var response = Execute(request); 
     return response.StatusCode; 
    } 

的问题是,它始终是returnig HTTP 422

请帮助。

+0

'422无法处理Entity' - 使用Fiddler看什么实际上是走出去,确保它看起来像你期望的那样。 – 2012-03-01 21:30:56

+0

我没有专家对HTTP,所以我wouldnt肯定如何期待它看起来......和提琴手是不是很友善的新手:( – ignaciofuentes 2012-03-01 21:32:36

+0

只是看着时间的应用程序。不知道为什么你不会只使用Quartz.Net可用http ://quartznet.sourceforge.net/ – Jeremy 2012-03-01 21:39:28

回答

1

所以这就是我最终的结果。 在这里找到一个样本 http://johnsheehan.me/blog/building-nugetlatest-in-two-hours-3/

public HttpStatusCode ScheduleTask(DateTime date, Uri url, string httpMethod, Uri callback = null) 
     { 
      var request = new RestRequest("jobs.json?apikey={apikey}&job[uri]={uri}&job[at]={at}&job[method]={method}", Method.POST); 
      request.AddUrlSegment("uri", "http://develop.myapp.com/Something"); 
      request.AddUrlSegment("at", "2012-03-31T18:36:21"); 
      request.AddUrlSegment("method", "GET"); 
      var response = Execute(request); 
      return response.StatusCode; 
     } 

林不完全确定的时候,我应该使用AddParameter,当我应该使用AddUrlSegment 但反正现在工作

+0

我之所以对该示例做网址段的原因是因为它们的API需要POST请求,而不是使用POST参数,而是使用URL参数,这是一个奇怪的组合。 – 2012-03-04 02:11:02

+0

gotcha。谢谢。 – ignaciofuentes 2012-03-05 18:34:55

+0

请记住,最好的方法在这个服务中传递一个时间是:'request.AddUrlSegment(“at”,nextRun.ToString(“yyyy-MM-ddTHH:mm:ss Z”));' – balexandre 2012-08-28 13:06:35

相关问题