Пример #1
0
        /// <summary>
        /// Transmits a block of data
        /// </summary>
        /// <param name="startIndex">Start index within the byte array</param>
        /// <param name="count">The number of bytes to send</param>
        /// <param name="final">Indicates whether this is the final packet</param>
        private void Transmit(long startIndex, long count, bool final)
        {
            string content = string.Empty;
            string mode    = "new";
            string url     = WebserviceURL + "/" + WebserviceMethod;
            Comms  helper  = null;

            if (_current.DataStream != null)
            {
                byte[] buffer = new byte[count];

                _current.DataStream.Read(buffer, 0, (int)count);
                content = Convert.ToBase64String(buffer);
            }
            else
            {
                content = Convert.ToBase64String(_current.Data, (int)startIndex, (int)count);
            }

            if (startIndex > 0)
            {
                mode = "append";
            }

            if (_current.TargetFileName.Length > 0)
            {
                helper = new Comms(new Uri(url), "POST", false,
                                   new KeyValuePair <string, string>("id", HttpUtility.UrlEncode(_current.ID)),
                                   new KeyValuePair <string, string>("mode", HttpUtility.UrlEncode(mode)),
                                   new KeyValuePair <string, string>("path", HttpUtility.UrlEncode(_current.TargetPath)),
                                   new KeyValuePair <string, string>("name", HttpUtility.UrlEncode(_current.FileName)),
                                   new KeyValuePair <string, string>("targetname", HttpUtility.UrlEncode(_current.TargetFileName)),
                                   new KeyValuePair <string, string>("filedata", HttpUtility.UrlEncode(content)),
                                   new KeyValuePair <string, string>("overwrite", Overwrite.ToString()),
                                   new KeyValuePair <string, string>("tag", HttpUtility.UrlEncode(_current.Tag)),
                                   new KeyValuePair <string, string>("final", final.ToString()));
            }
            else
            {
                helper = new Comms(new Uri(url), "POST", false,
                                   new KeyValuePair <string, string>("id", HttpUtility.UrlEncode(_current.ID)),
                                   new KeyValuePair <string, string>("mode", HttpUtility.UrlEncode(mode)),
                                   new KeyValuePair <string, string>("path", HttpUtility.UrlEncode(_current.TargetPath)),
                                   new KeyValuePair <string, string>("name", HttpUtility.UrlEncode(_current.FileName)),
                                   new KeyValuePair <string, string>("filedata", HttpUtility.UrlEncode(content)),
                                   new KeyValuePair <string, string>("overwrite", Overwrite.ToString()),
                                   new KeyValuePair <string, string>("tag", HttpUtility.UrlEncode(_current.Tag)),
                                   new KeyValuePair <string, string>("final", final.ToString()));
            }
            helper.ResponseComplete += new HttpResponseCompleteEventHandler(this.CommandComplete);
            helper.Execute();
        }
Пример #2
0
        private static void BeginRequest(IAsyncResult ar)
        {
            Comms helper = ar.AsyncState as Comms;

            if (helper != null)
            {
                using (StreamWriter writer = new StreamWriter(helper.Request.EndGetRequestStream(ar)))
                {
                    foreach (var item in helper.PostValues)
                    {
                        writer.Write("{0}={1}&", item.Key, item.Value);
                    }
                }
                helper.Request.BeginGetResponse(new AsyncCallback(Comms.BeginResponse), helper);
            }
        }
Пример #3
0
        private static void BeginResponse(IAsyncResult ar)
        {
            Comms helper = ar.AsyncState as Comms;

            if (helper != null)
            {
                HttpWebResponse response = (HttpWebResponse)helper.Request.EndGetResponse(ar);
                if (response != null)
                {
                    Stream stream = response.GetResponseStream();
                    if (stream != null)
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            helper.OnResponseComplete(new HttpResponseCompleteEventArgs(reader.ReadToEnd(), helper.Tag));
                        }
                    }
                }
            }
        }