public static string CutJSONHeader(string json) { if (!CheckStringIsAPIException(json)) { return string.Empty; }//end if return json.Substring(JSONHeader.Length, json.Length - JSONHeader.Length); }//end method
/// <summary> /// 以同步的形式发起HTTP请求并返回类型为泛型的API响应对象。 /// </summary> /// <typeparam name="T">定义结果的泛型。</typeparam> /// <returns>返回结果类型为泛型的API响应对象。</returns> public virtual APIResponse <T> ToAPIResponse <T>() { var result = new APIResponse <T>(); try { var response = Post().asString(); result.StatusCode = (HttpStatusCode)response.Code; var json = response.Body; //判断返回的是否是一个异常数据包 //如果是的,反序列化之,并引发一个异常 //反之正常反序列化 if (APIException.CheckStringIsAPIException(json)) { result.APIError = JsonConvert.DeserializeObject <APIException>(APIException.CutJSONHeader(json)); throw result.APIError.ToException();//这里会引发异常 }//end if if (typeof(T).Equals(typeof(string))) { result.Body = (T)(object)json; } else { result.Body = JsonConvert.DeserializeObject <T>(json); } } catch (Exception ex) { result.Error = ex; } return(result); }//end method