}//end method /// <summary> /// 运行响应任务。 /// </summary> /// <param name="ar">异步结果对象。</param> protected internal void RunResponseTask(IAsyncResult ar) { try { //获取HTTP异步响应对象 this.HttpResponse = (HttpWebResponse)this.HttpRequest.EndGetResponse(ar); this.Result.StatusCode = this.HttpResponse.StatusCode; //接收json字符串 var json = this.ClientRequest.HttpUtility.GetResponseString(this.HttpResponse); //判断返回的是否是一个异常数据包 //如果是的,反序列化之,并引发一个异常 //反之正常反序列化 if (APIException.CheckStringIsAPIException(json)) { this.Result.APIError = JsonConvert.DeserializeObject <APIException>(APIException.CutJSONHeader(json)); throw this.Result.APIError.ToException(); }//end if if (typeof(T).Equals(typeof(string))) { this.Result.Body = (T)(object)json; } else { this.Result.Body = JsonConvert.DeserializeObject <T>(json); } }//end try catch (Exception ex) { this.Result.Error = ex; } if (this.Result.Error != null) { if (this.FailCallBack != null) { this.FailCallBack(this.Result); } } else { if (this.SuccessCallBack != null) { this.SuccessCallBack(this.Result); } } }//end method
}//end method /// <summary> /// 结束Unirest异步调用并获取异步任务对象。 /// </summary> /// <param name="task">异步任务对象。</param> internal void EndUnirestPostAsJson(Task <HttpResponse <string> > task) { #region TaskCanceled //如果异步任务被取消,则什么都不做。 if (task.IsCanceled) { return; }//end if #endregion #region TaskCompleted //如果成功完成,则获取结果。 if (task.IsCompleted) { this.Result.StatusCode = (HttpStatusCode)task.Result.Code; var json = task.Result.Body; try { //判断返回的是否是一个异常数据包 //如果是的,反序列化之,并引发一个异常 //反之正常反序列化 if (APIException.CheckStringIsAPIException(json)) { this.Result.APIError = JsonConvert.DeserializeObject <APIException>(APIException.CutJSONHeader(json)); this.Result.Error = this.Result.APIError.ToException(); } else { if (typeof(T).Equals(typeof(string))) { this.Result.Body = (T)(object)json; } else { this.Result.Body = JsonConvert.DeserializeObject <T>(json); } } //end if else } //end try catch (Exception ex) { this.Result.Error = ex; }//end catch //最终根据是否有异常来执行相应的回调方法。 if (this.Result.Error == null) { if (this.SuccessCallBack != null) { this.SuccessCallBack(this.Result); }//end if } else { if (this.FailCallBack != null) { this.FailCallBack(this.Result); } //end if } //end if else } //end if #endregion #region TaskFaulted //如果发生异常,则捕获异常。 if (task.IsFaulted) { this.Result.Error = task.Exception; this.Result.StatusCode = (HttpStatusCode)task.Result.Code; if (this.FailCallBack != null) { this.FailCallBack(this.Result); } //end if } //end if #endregion }//end method