コード例 #1
0
        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
                    {
                        //this.myRequestState = _myRequestState;
                        RaiseEvents(_myRequestState);
                    }
                }
            }
            #region 异常处理

            catch (Exception e)
            {
                Debug.WriteLine(
                    string.Format("HttpWebConnect.ReadCallBack  -> error = {0}"
                                  , e.Message));
                this.RaiseException(e);
                //if (this.AcceptException != null)
                //{
                //    HttpException he = new HttpException();
                //    he.Message = e.Message;
                //    this.AcceptException(he);

                //}
            }

            #endregion

            ReleaseSource();
        }
コード例 #2
0
 public HttpWebConnect()
 {
     myRequestState = new RequestState();
 }
コード例 #3
0
 public HttpWebConnect()
 {
     myRequestState = new RequestState();
 }
コード例 #4
0
 private void RequestStreamCallBack(IAsyncResult asynchronousResult)
 {
     try
     {
         myRequestState = (RequestState)asynchronousResult.AsyncState;
         HttpWebRequest myrequest = myRequestState.request;
         // End the operation.
         Stream postStream = myrequest.EndGetRequestStream(asynchronousResult);
         UTF8Encoding enc = new UTF8Encoding();
         byte[] byteArray = enc.GetBytes(this.postData);
         //myrequest.ContentLength = byteArray.Length;
         // Write to the request stream.
         postStream.Write(byteArray, 0, postData.Length);
         postStream.Close();
         myrequest.BeginGetResponse(new AsyncCallback(RespCallback), myRequestState);
     }
     catch (System.Exception ex)
     {
         this.RaiseException(ex);
     }
 }