当响应头有位置字段时,RestSharp 返回空值 [英] RestSharp returns null value when response header has location field

查看:67
本文介绍了当响应头有位置字段时,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 检查响应时,原始数据为:

When checking response through Fiddler, raw data is:

    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 有关系.以下是成功解析响应的原始数据:

though RestSharp shows all respose values as null. I asume that this has something to with default JSonDeserializer, who can not parse some of the data. Below is raw data with successfull parsed response:

    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 状态或位置值或其他原因吗?我对此感到非常沮丧,几乎一天都没有成功:(

Can this be because of http status or location value or something else? Im really frustrated about this, spet allmost a day without success :(

推荐答案

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

I found a solution to my problem in this post: https://stackoverflow.com/a/1028141/1341545

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

Eventually you must set Restsharp client FollowRedirects property to "false" so that the client ignores request headers redirect location.

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

成功

这篇关于当响应头有位置字段时,RestSharp 返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆