Ejemplo n.º 1
0
        static void OpenWrite(IAsyncResult ar)
        {
            RequestState state = (RequestState)ar.AsyncState;

            try
            {
                // Get the stream to write our upload to
                using (Stream uploadStream = state.Request.EndGetRequestStream(ar))
                {
                    // Fire the callback for successfully opening the stream
                    if (state.OpenWriteCallback != null)
                    {
                        state.OpenWriteCallback(state.Request);
                    }

                    // Write our data to the upload stream
                    uploadStream.Write(state.UploadData, 0, state.UploadData.Length);
                }

                // Start the request for the remote server response
                IAsyncResult result = state.Request.BeginGetResponse(GetResponse, state);
                // Register a timeout for the request
                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, TimeoutCallback, state,
                                                       state.MillisecondsTimeout, true);
            }
            catch (Exception ex)
            {
                //Logger.Log.Debug("CapsBase.OpenWrite(): " + ex.Message);
                if (state.CompletedCallback != null)
                {
                    state.CompletedCallback(state.Request, null, null, ex);
                }
            }
        }
Ejemplo n.º 2
0
        void GetResponse(IAsyncResult ar)
        {
            RequestState    state    = (RequestState)ar.AsyncState;
            HttpWebResponse response = null;
            Exception       error    = null;
            String          result   = "";

            try
            {
                response = (HttpWebResponse)state.Request.EndGetResponse(ar);
                using (Stream responseStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream);

                    result = reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }

            if (state.CompletedCallback != null)
            {
                state.CompletedCallback(state.Request, response, result, error);
            }
        }
Ejemplo n.º 3
0
        void OpenWrite(IAsyncResult ar)
        {
            RequestState state = (RequestState)ar.AsyncState;

            try
            {
                // Get the stream to write our upload to
                using (Stream uploadStream = state.Request.EndGetRequestStream(ar))
                {
                    byte[] buffer = new Byte[checked ((uint)Math.Min(1024, (int)state.UploadData.Length))];

                    MemoryStream ms = new MemoryStream(state.UploadData);

                    int bytesRead;
                    int i = 0;
                    while ((bytesRead = ms.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        int prog = (int)Math.Floor(Math.Min(100.0,
                                                            (((double)(bytesRead * i) / (double)ms.Length) * 100.0)));


                        uploadStream.Write(buffer, 0, bytesRead);

                        i++;

                        if (state.ProgressCallback != null)
                        {
                            state.ProgressCallback(state.Request, prog);
                        }
                    }

                    if (state.ProgressCallback != null)
                    {
                        state.ProgressCallback(state.Request, 100);
                    }

#if !NETFX_CORE
                    ms.Close();
                    uploadStream.Close();
#endif
                }

                IAsyncResult result = state.Request.BeginGetResponse(GetResponse, state);

#if !NETFX_CORE
                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, TimeoutCallback, state,
                                                       state.MillisecondsTimeout, true);
#endif
            }
            catch (Exception ex)
            {
                if (state.CompletedCallback != null)
                {
                    state.CompletedCallback(state.Request, null, null, ex);
                }
            }
        }
Ejemplo n.º 4
0
        void OpenWrite(IAsyncResult ar)
        {
            RequestState state = (RequestState)ar.AsyncState;

            try
            {
                // Get the stream to write our upload to
                using (Stream uploadStream = state.Request.EndGetRequestStream(ar))
                {
                    byte[] buffer = new Byte[checked ((uint)Math.Min(1024, (int)state.UploadData.Length))];

                    MemoryStream ms = new MemoryStream(state.UploadData);

                    int bytesRead;
                    int i = 0;
                    while ((bytesRead = ms.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        int prog = (int)Math.Floor(Math.Min(100.0,
                                                            (((double)(bytesRead * i) / (double)ms.Length) * 100.0)));


                        uploadStream.Write(buffer, 0, bytesRead);

                        i++;
                    }
                }

                IAsyncResult result = state.Request.BeginGetResponse(GetResponse, state);
            }
            catch (Exception ex)
            {
                if (state.CompletedCallback != null)
                {
                    state.CompletedCallback(state.Request, null, null, ex);
                }
            }
        }
Ejemplo n.º 5
0
        static void GetResponse(IAsyncResult ar)
        {
            RequestState    state    = (RequestState)ar.AsyncState;
            HttpWebResponse response = null;

            byte[]    responseData = null;
            Exception error        = null;

            try
            {
                response = (HttpWebResponse)state.Request.EndGetResponse(ar);
                // Get the stream for downloading the response
                using (Stream responseStream = response.GetResponseStream())
                {
                    #region Read the response

                    // If Content-Length is set we create a buffer of the exact size, otherwise
                    // a MemoryStream is used to receive the response
                    bool         nolength = (response.ContentLength <= 0);
                    int          size     = (nolength) ? 8192 : (int)response.ContentLength;
                    MemoryStream ms       = (nolength) ? new MemoryStream() : null;
                    byte[]       buffer   = new byte[size];

                    int bytesRead      = 0;
                    int offset         = 0;
                    int totalBytesRead = 0;
                    int totalSize      = nolength ? 0 : size;

                    while ((bytesRead = responseStream.Read(buffer, offset, size)) != 0)
                    {
                        totalBytesRead += bytesRead;

                        if (nolength)
                        {
                            totalSize += (size - bytesRead);
                            ms.Write(buffer, 0, bytesRead);
                        }
                        else
                        {
                            offset += bytesRead;
                            size   -= bytesRead;
                        }

                        // Fire the download progress callback for each chunk of received data
                        if (state.DownloadProgressCallback != null)
                        {
                            state.DownloadProgressCallback(state.Request, response, totalBytesRead, totalSize);
                        }
                    }

                    if (nolength)
                    {
                        responseData = ms.ToArray();
                        ms.Close();
                        ms.Dispose();
                    }
                    else
                    {
                        responseData = buffer;
                    }

                    #endregion Read the response

                    responseStream.Close();
                }
            }
            catch (Exception ex)
            {
                // Logger.DebugLog("CapsBase.GetResponse(): " + ex.Message);
                error = ex;
            }

            if (state.CompletedCallback != null)
            {
                state.CompletedCallback(state.Request, response, responseData, error);
            }
        }