簡體   English   中英

當響應頭具有位置字段時,RestSharp返回空值

[英]RestSharp returns null value when response header has location field

我的休息要求:

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狀態或位置值或其他原因? 我真的很沮喪,花了一整天沒有成功:(

我在這篇文章中找到了解決問題的方法: https//stackoverflow.com/a/1028141/1341545

最終,您必須將Restsharp客戶端FollowRedirects屬性設置為“false”,以便客戶端忽略請求標頭重定向位置。

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

訣竅

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM