private void ReadCallBack(IAsyncResult asyncResult) { if (this.IsCancled) { this.ReleaseSource(); return; } try { //RequestState myRequestState = (RequestState)asyncResult.AsyncState; Stream responseStream = myRequestState.streamResponse; int read = responseStream.EndRead(asyncResult); // Read the HTML page and then print it to the console. if (read > 0) { //string contentType = myRequestState.response.ContentType; //string encodeName = contentType.Substring(contentType.IndexOf("charset") + 8); //Encoding ecd = Encoding.GetEncoding(encodeName); //myRequestState.requestData.Append(ecd.GetString(myRequestState.BufferRead, 0, read)); myRequestState.requestData.Append(Encoding.UTF8.GetString(myRequestState.BufferRead, 0, read)); IAsyncResult asynchronousResult = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState); return; } else { // 数据从网络上读取完毕,此时需要完成两个工作: // 1. 检查数据中是否有自定义的异常信息 // 2.如果没有异常信息,调用完成事件 string str = myRequestState.requestData.ToString(); if (myErrorParser != null) { if (myErrorParser.HasError(str)) { HttpException he = new HttpException(); he.Message = myErrorParser.GetErrorMessage(str); this.RaiseException(he); //if (this.AcceptException != null) // this.AcceptException(he); } else this.RaiseEvents(); } else RaiseEvents(); } } #region 异常处理 catch (Exception e) { this.RaiseException(e); //if (this.AcceptException != null) //{ // HttpException he = new HttpException(); // he.Message = e.Message; // this.AcceptException(he); //} } #endregion ReleaseSource(); }
void RaiseException(Exception e) { if (this.AcceptException != null) { HttpException he = new HttpException(); he.Message = e.Message; //this.AcceptException(he); } }