【问题标题】:RestSharp RestResponse is truncating content to 64 kbRestSharp RestResponse 将内容截断为 64 kb
【发布时间】:2014-08-29 14:35:04
【问题描述】:

您好,我正在使用 RestSharp 创建对我的 Web API 的请求。不幸的是,response.content 不包含完整的响应,当我通过浏览器或提琴手执行请求时,我可以看到它。内容被截断为 64 kb。我在下面附上我的代码。

您能建议什么可以解决这个问题吗?

var request = new RestRequest("Products?productId={productId}&applicationId={applicationId}", Method.GET);
request.RequestFormat = DataFormat.Json;
request.AddParameter("productId", id, ParameterType.UrlSegment);
request.AddParameter("applicationId", Settings.ApplicationId, ParameterType.UrlSegment);
request.AddHeader("X-AppKey", token.AppKey);
request.AddHeader("X-Token", token.Token);
request.AddHeader("X-IsWebApi", "true");

RestResponse response = (RestResponse) client.Execute(request);

if (response.StatusCode == HttpStatusCode.Found)
{
    // The following line failes because response.Content is truncated.
    ShowProductModel showProductModel =
        new JavaScriptSerializer().Deserialize<ShowProductModel>(response.Content);

   // Do other things.
   return ShowProductApi(showProductModel, q, d, sort, breadcrumb);
}

【问题讨论】:

    标签: httprequest httpresponse restsharp


    【解决方案1】:

    这是因为 RestSharp 使用 .NET Framework 中的 HttpWebRequest 类。此类具有称为 DefaultMaximumErrorResponseLength 的静态属性。该属性决定了错误响应的最大长度,该属性的默认值为64Kb。

    您可以在实例化 RestRequest 类之前更改该属性的值。

    这里有一些代码:

     HttpWebRequest.DefaultMaximumErrorResponseLength = 1048576;
    
     var request = new RestRequest("resource" + "/", Method.POST)
        {
            RequestFormat = DataFormat.Json,
            JsonSerializer = new JsonSerializer()
        };
    

    这样你的错误响应可以更长而没有问题。

    【讨论】:

      【解决方案2】:

      看起来HttpStatusCode.Found 可能是导致问题的原因。这相当于 Http Status Code 302,它是一种重定向形式。我不完全确定在这种情况下这是否一定是正确的做法。如果您“找到”了您要查找的数据,则应返回成功级别状态代码,例如200(好的)。维基百科有一个list of HTTP Status Codes,其中包含关于它们的含义的摘要,并链接到许多其他资源。

      我创建了一个小演示解决方案(您可以找到它on GitHub)来展示差异。有一个返回值列表(十六进制代码)的 WebApi 服务器应用程序和一个消耗 WebApi 应用程序资源的控制台客户端应用程序。

      这是返回 HTTP 状态代码 302/Found 的 ValuesFound 资源:

      public class ValuesFoundController : ApiController
      {
          public HttpResponseMessage Get(int count)
          {
              var result = Request.CreateResponse(HttpStatusCode.Found, Values.GetValues(count));
              return result;
          }
      }
      

      同样如此,但返回正确的 200/OK 响应:

      public class ValuesOkController : ApiController
      {
          public HttpResponseMessage Get(int count)
          {
              var result = Request.CreateResponse(HttpStatusCode.OK, Values.GetValues(count));
              return result;
          }
      }
      

      在客户端,代码的重要部分是这样的:

      private static void ProcessRequest(int count, string resource)
      {
          var client = new RestClient("http://localhost:61038/api/");
          var request = new RestRequest(resource+"?count={count}", Method.GET);
          request.RequestFormat = DataFormat.Json;
          request.AddParameter("count", count, ParameterType.UrlSegment);
          RestResponse response = (RestResponse) client.Execute(request);
          Console.WriteLine("Status was                : {0}", response.StatusCode);
          Console.WriteLine("Status code was           : {0}", (int) response.StatusCode);
          Console.WriteLine("Response.ContentLength is : {0}", response.ContentLength);
          Console.WriteLine("Response.Content.Length is: {0}", response.Content.Length);
          Console.WriteLine();
      }
      

      count 是要返回的十六进制代码的数量,resource 是映射到上述控制器的资源名称(ValuesOkValuesFound)。

      控制台应用程序要求用户输入一个数字,然后显示每个 HTTP 状态代码的响应长度。对于较低的值,比如 200,两个版本都返回相同数量的内容,但是一旦响应内容超过 64kb,“找到”版本就会被截断,而“确定”版本则不会。

      尝试使用值约为 9999 的控制台应用程序证明了这一点:

      How many things do you want returned?
      9999
      Waiting on the server...
      Status was                : OK
      Status code was           : 200
      Response.ContentLength is : 109990
      Response.Content.Length is: 109990
      
      Status was                : Redirect
      Status code was           : 302
      Response.ContentLength is : 109990
      Response.Content.Length is: 65536
      

      那么,RestSharp 为什么要这样做呢?我不知道为什么它会在一个实例中截断内容而不是在另一个实例中截断内容。但是,可以假设在服务器要求客户端重定向到另一个资源位置的情况下,超过 64kb 的内容不太可能是有效的。

      例如,如果您使用 Fiddler 查看网站的操作,则 300 范围(重定向)中的响应(例如 302/Found)确实有一个小的内容负载,其中仅包含一些 HTML,以便用户可以单击如果浏览器没有为它们自动重定向,则链接手动重定向。真正的重定向在 Http 的“Location”标头中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-03
        • 1970-01-01
        • 2021-01-11
        • 2017-03-23
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        相关资源
        最近更新 更多