Exemplo n.º 1
0
        ///
        /// ------------------------------------------------------------------------------------------------
        /// Name		UploadDataAsync
        ///
        /// <summary>	Asynchronously uploads data to the url.
        /// </summary>
        /// <param name="url">		The url for the request.</param>
        /// <param name="isPut">	Whether the method is PUT or POST.</param>
        /// <param name="data">		The data content for the request.</param>
        /// <param name="contentType"></param>
        /// <param name="firstTry">	Indicates that this is the first try at the request.</param>
        ///
        /// <remarks>
        /// </remarks>
        /// ------------------------------------------------------------------------------------------------
        public async Task <RequestResponseEventArgs> UploadDataAsync(string url,
                                                                     bool isPut,
                                                                     byte[] data,
                                                                     ContentType contentType,
                                                                     bool firstTry = true)
        {
            HttpClient client;
            RequestResponseEventArgs args;
            HttpResponseMessage      result;

            byte[] response;
            string stringException;

            try
            {
                client   = new HttpClient(new NativeMessageHandler());
                StatCode = HttpStatusCode.InternalServerError;

                foreach (string name in m_oHeaders.Keys)
                {
                    client.DefaultRequestHeaders.Add(name, m_oHeaders[name]);
                }
                ByteArrayContent byteContent = new ByteArrayContent(data);
                byteContent.Headers.Add("Content-Type", GetContentType(contentType));
                result = isPut ? await client.PutAsync(url, byteContent) : await client.PostAsync(url, byteContent);

                if (result.IsSuccessStatusCode)
                {
                    response = await result.Content.ReadAsByteArrayAsync();

                    args = new RequestResponseEventArgs(response);
                }
                else
                {
                    StatCode = result.StatusCode;
                    if (result.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        throw new HttpRequestException();
                    }
                    else
                    {
                        if (result.Content != null)
                        {
                            stringException = await result.Content.ReadAsStringAsync();
                        }
                        else
                        {
                            stringException = result.ReasonPhrase;
                        }

                        throw new Exception(stringException);
                    }
                }

                args = new RequestResponseEventArgs(response);
            }
            catch (HttpRequestException ex)
            {
                if (firstTry && ex.Message.Contains("(ReadDone2)"))
                {
                    await Task.Delay(500);

                    return(await UploadDataAsync(url, isPut, data, contentType, false));
                }
                args             = new RequestResponseEventArgs(ex, url);
                args.Body        = string.Format("Data content of length [{0}].", data.Length);
                args.RequestType = isPut ? "PUT" : "POST";
                LogTracking.LogTrace(ex.ToString());
            }
            catch (Exception ex)
            {
                args = new RequestResponseEventArgs(ex, url);
                LogTracking.LogTrace(ex.ToString());
            }

            return(args);
        }
Exemplo n.º 2
0
        ///
        /// ------------------------------------------------------------------------------------------------
        /// Name		UploadStringAsync
        ///
        /// <summary>	Asynchronously uploads string content to the url.
        /// </summary>
        /// <param name="url">		The url for the request.</param>
        /// <param name="isPut">	Where the method is PUT or POST.</param>
        /// <param name="content">	The string content of the request.</param>
        /// <param name="contentType"></param>
        /// <param name="firstTry">	Indicates that this is the first try at the request.</param>
        ///
        /// <remarks>
        /// </remarks>
        /// ------------------------------------------------------------------------------------------------
        public async Task <RequestResponseEventArgs> UploadStringAsync(string url,
                                                                       bool isPut,
                                                                       string content,
                                                                       ContentType contentType,
                                                                       bool firstTry = true)
        {
            HttpClient client;
            RequestResponseEventArgs args;
            string response;

            //HttpResponseMessage result;
            //string response;
            //
            try
            {
                client   = new HttpClient(new NativeMessageHandler());
                StatCode = HttpStatusCode.InternalServerError;
                foreach (string name in m_oHeaders.Keys)
                {
                    client.DefaultRequestHeaders.Add(name, m_oHeaders[name]);
                }
                //
                StringContent       stringContent = new StringContent(content, Encoding.UTF8, GetContentType(contentType));
                HttpResponseMessage result        = isPut ? await client.PutAsync(url, stringContent) : await client.PostAsync(url, stringContent);

                if (result.IsSuccessStatusCode)
                {
                    response = await result.Content.ReadAsStringAsync();

                    args = new RequestResponseEventArgs(response);
                }
                else
                {
                    StatCode = result.StatusCode;
                    if (result.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        throw new HttpRequestException();
                    }
                    else
                    {
                        if (result.Content != null)
                        {
                            response = await result.Content.ReadAsStringAsync();
                        }
                        else
                        {
                            response = result.ReasonPhrase;
                        }
                        //
                        throw new Exception(response);
                    }
                }
            }
            catch (HttpRequestException ex)
            {
                if (firstTry && ex.Message.Contains("(ReadDone2)"))
                {
                    await Task.Delay(500);

                    return(await UploadStringAsync(url, isPut, content, contentType, false));
                }
                args = new RequestResponseEventArgs(ex, url);
                LogTracking.LogTrace(ex.ToString());
            }
            catch (Exception ex)
            {
                args = new RequestResponseEventArgs(ex, url);
                LogTracking.LogTrace(ex.ToString());
            }
            //
            if (args.Error != null)
            {
                args.Body        = content;
                args.RequestType = isPut ? "PUT" : "POST";
            }
            //
            return(args);
        }
Exemplo n.º 3
0
        ///
        /// ------------------------------------------------------------------------------------------------
        /// Name		DownloadDataAsync
        ///
        /// <summary>	Asynchronously downloads binary data from a url.
        /// </summary>
        /// <param name="url">		The Url for the request.</param>
        /// <param name="firstTry">	Indicates that this is the first try at the request.</param>
        ///
        /// <remarks>
        /// </remarks>
        /// ------------------------------------------------------------------------------------------------
        ///
        public async Task <RequestResponseEventArgs> DownloadDataAsync(string url, bool firstTry = true)
        {
            HttpClient client;
            RequestResponseEventArgs args;

            byte[] response;
            string stringException;

            //
            try
            {
                client   = new HttpClient(new NativeMessageHandler());
                StatCode = HttpStatusCode.InternalServerError;
                foreach (string name in m_oHeaders.Keys)
                {
                    client.DefaultRequestHeaders.Add(name, m_oHeaders[name]);
                }
                var result = await client.GetAsync(url);

                if (result.IsSuccessStatusCode)
                {
                    response = await result.Content.ReadAsByteArrayAsync();

                    args = new RequestResponseEventArgs(response);
                }
                else
                {
                    StatCode = result.StatusCode;
                    if (result.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        throw new HttpRequestException();
                    }
                    else
                    {
                        if (result.Content != null)
                        {
                            stringException = await result.Content.ReadAsStringAsync();
                        }
                        else
                        {
                            stringException = result.ReasonPhrase;
                        }

                        throw new Exception(stringException);
                    }
                }
            }
            catch (HttpRequestException ex)
            {
                if (firstTry && ex.Message.Contains("(ReadDone2)"))
                {
                    await Task.Delay(500);

                    return(await DownloadDataAsync(url, false));
                }

                args = new RequestResponseEventArgs(ex, url);
                LogTracking.LogTrace(ex.ToString());
            }
            catch (Exception ex)
            {
                args = new RequestResponseEventArgs(ex, url);
                LogTracking.LogTrace(ex.ToString());
            }
            //
            return(args);
        }