Esempio n. 1
0
        private static void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            UploadState state = (UploadState)asynchronousResult.AsyncState;

            state.statusCB("Waiting For Response");

            // End the operation
            try
            {
                HttpWebResponse response       = (HttpWebResponse)state.request.EndGetResponse(asynchronousResult);
                Stream          streamResponse = response.GetResponseStream();
                StreamReader    streamRead     = new StreamReader(streamResponse);
                state.response = streamRead.ReadToEnd();
                // Close the stream object
                streamResponse.Close();
                streamRead.Close();
                response.Close();
            }
            catch (Exception e)
            {
                state.failureCB(e, "Result Retrieval Problem");
                return;
            }
            // Release the HttpWebResponse

            state.statusCB("Done");
            state.responseDoneCB();
        }
Esempio n. 2
0
        private void postFile_GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {
            UploadState state = (UploadState)asynchronousResult.AsyncState;

            state.statusCB("Sending File");
            // End the operation
            try
            {
                Stream    postStream = state.request.EndGetRequestStream(asynchronousResult);
                const int BUFFER     = 4096;

                byte[]     buffer            = new byte[BUFFER];
                FileStream encodedFileStream = File.OpenRead(state.filename);
                int        bytes_read        = 0;
                long       total_read        = 0;
                bytes_read = encodedFileStream.Read(buffer, 0, BUFFER);
                while (bytes_read > 0)
                {
                    total_read += bytes_read;
                    postStream.Write(buffer, 0, bytes_read);
                    bytes_read = encodedFileStream.Read(buffer, 0, BUFFER);
                    state.progCB(((double)total_read / (double)state.totalBytes) * 100.0);
                }

                postStream.Close();
                encodedFileStream.Close();
                File.Delete(state.filename);
            }

            catch (Exception e)
            {
                state.failureCB(e, "Upload Problem");
                return;
            }
            state.uploadDoneCB();
            // Start the asynchronous operation to get the response
            state.request.BeginGetResponse(new AsyncCallback(GetResponseCallback), state);
        }