当响应头具有位置字段时,RestSharp返回空值

时间:2012-12-05 12:18:19

标签: c# windows-phone-7 restsharp

我的休息请求:

RestSharp.RestClient uplClient = new RestSharp.RestClient();
RestSharp.RestRequest request = new RestSharp.RestRequest(IMAGE_UPLOAD_URI, Method.POST);
request.AddParameter("user", USER_HASH);
request.AddParameter("apikey", API_KEY);
request.AddFile("Filedata", file, "test.jpg","image/jpeg");

uplClient.ExecuteAsync(request, (response) =>
 {
  callback(response.Content, null);

  if (response.StatusCode == HttpStatusCode.OK)
  {
   MessageBox.Show("Upload completed succesfully...\n" + response.Content);
  }
  else
  {
   MessageBox.Show(response.StatusCode + "\n" + response.StatusDescription);
  }
 });

通过Fiddler检查响应时,原始数据为:

    HTTP/1.1 302 Moved Temporarily
    Server: nginx
    Date: Wed, 05 Dec 2012 11:35:57 GMT
    Content-Type: text/javascript;charset=utf-8
    Connection: keep-alive
    Location: file:///Applications/Install/8014A556-A76A-4294-B375-6E3668177CCA/Install/?errorNr=0&picUploadId=1007501
    Content-Length: 423

    {"ok":true,"error":false,"imageIcon":0,"uid":1287837,"tmpId":1007501,"url":"http:\/\/i1.ifrype.com\/tmp\/10\/1007501.jpg","urlIcon":"http:\/\/i1.ifrype.com\/tmp\/10\/i_1007501.jpg","urlSmall":"http:\/\/i1.ifrype.com\/tmp\/10\/sm_1007501.jpg","urlMiddle":"http:\/\/i1.ifrype.com\/tmp\/10\/nm_1007501.jpg","urlLarge":"http:\/\/i1.ifrype.com\/tmp\/10\/l_1007501.jpg","urlGM":"http:\/\/i1.ifrype.com\/tmp\/10\/ngm_1007501.jpg"}

虽然RestSharp将所有respose值显示为null。我认为这与默认的JSonDeserializer有关,它无法解析某些数据。下面是成功解析响应的原始数据:

    HTTP/1.1 200 OK
    Server: nginx
    Date: Wed, 05 Dec 2012 11:28:13 GMT
    Content-Type: text/javascript;charset=utf-8
    Connection: keep-alive
    Content-Length: 1015

    {"users_login":{"login":{"apikey":"9073902hjkwehfweiufy34","img":"http:\/\/i7.ifrype.com\/profile\/287\/837\/v1341214689\/sm_12858487837.jpg"}}}

这可能是因为http状态或位置值或其他原因吗?我真的很沮丧,花了一天时间没有成功:(

1 个答案:

答案 0 :(得分:6)

我在这篇文章中找到了解决问题的方法:https://stackoverflow.com/a/1028141/1341545

最终,您必须将Restsharp客户端FollowRedirects属性设置为“false”,以便客户端忽略请求标头重定向位置。

RestSharp.RestClient uplClient = new RestSharp.RestClient();
uplClient.FollowRedirects = false;

诀窍