2014-08-29 54 views
1

嗨我正在使用RestSharp創建對我的Web API的請求。不幸的是,response.content不包含完整的響應,當我通過瀏覽器或提琴手執行請求時,我可以看到它。內容正被截斷爲64 kb。我在下面附上我的代碼。RestSharp RestResponse截斷內容爲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); 
} 

回答

1

看起來像HttpStatusCode.Found可能會導致問題。這相當於Http狀態碼302,它是一種重定向形式。我不完全確定在這種情況下這是否是正確的做法。如果您「找到」了您要查找的數據,則應返回成功級別狀態代碼,例如200(好)。維基百科有一個list of HTTP Status Codes摘要關於他們的意思,並鏈接到其他資源。

我已經創建了一個小演示解決方案(您可以找到它on GitHub)以顯示不同之處。有一個WebApi服務器應用程序返回值列表(十六進制代碼)和一個控制檯客戶端應用程序,它們消耗WebApi應用程序上的資源。

這裏是ValuesFound資源返回HTTP狀態代碼302 /實測值:再次而是返回正確的200/OK響應

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

而且是相同的:

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是n十六進制代碼返回,resource是映射到上述控制器的資源名稱(ValuesOkValuesFound)。

控制檯應用程序詢問用戶一個號碼,然後顯示每個HTTP狀態碼的響應時間長度。對於低值(比如200),兩個版本都會返回相同數量的內容,但一旦響應內容超過64kb,「Found」版本將被截斷,而「Ok」版本將不會被截斷。

試圖控制檯應用程序約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」標題中。

3

發生這種情況的原因是RestSharp使用.NET Framework中的HttpWebRequest類。這個類有一個名爲DefaultMaximumErrorResponseLength的靜態屬性。此屬性確定錯誤響應的最大長度,此屬性的默認值爲64Kb。

您可以在安裝RestRequest類之前更改該atribbute的值。

下面是一些代碼:

HttpWebRequest.DefaultMaximumErrorResponseLength = 1048576; 

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

這樣,你的錯誤響應可以更長,而不problemns。