public static WazombiNetworkingResponse WithCookies(this WazombiNetworkingResponse response, CookieCollection cookies)
 {
     if (cookies != null && cookies.Count > 0)
     {
         response.Cookies = cookies;
     }
     return(response);
 }
 public static WazombiNetworkingResponse WithJson(this WazombiNetworkingResponse response, JsonValue json)
 {
     response.Json = json;
     return(response);
 }
 public static WazombiNetworkingResponse WithException(this WazombiNetworkingResponse response, int exception)
 {
     response.Exception = exception;
     return(response);
 }
 public static WazombiNetworkingResponse WithStatus(this WazombiNetworkingResponse response, HttpStatusCode statusCode)
 {
     response.StatusCode = statusCode;
     return(response);
 }
        public async static Task <WazombiNetworkingResponse> SendData(WazombiNetworkingRequest requestInfo)
        {
            LogNetworking("WazombiNetworking SendData: " + requestInfo.Url + " - " + requestInfo.Method);

            string         url     = requestInfo.Url;
            Uri            uri     = new Uri(url);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

            byte[] byteArray = requestInfo.HasJson ? Encoding.UTF8.GetBytes(requestInfo.Json.ToString()) : new byte[0];

            request.ContentLength          = byteArray.Length;
            request.Method                 = requestInfo.Method;
            request.Timeout                = TIMEOUT_IN_MILLISECONDS;
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            if (requestInfo.HasHeaders)
            {
                request.Headers = requestInfo.Headers;
            }

            if (requestInfo.HasCookies)
            {
                CookieContainer cookieContainer = new CookieContainer();
                foreach (Cookie cookie in requestInfo.Cookies)
                {
                    cookie.Domain = uri.Host;
                    cookieContainer.Add(cookie);
                }
                request.CookieContainer = cookieContainer;
            }
            else
            {
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(uri, new CookieCollection());
            }

            WazombiNetworkingResponse result = new WazombiNetworkingResponse();

            if (request.Method == POST || request.Method == PUT)
            {
                if (requestInfo.HasJson)
                {
                    request.ContentType = "application/json";
                    request.Accept      = "application/json";
                }
                request.AllowAutoRedirect = false;

                Stream dataStream;

                try
                {
                    dataStream = await request.GetRequestStreamAsync().WithTimeout(TIMEOUT_IN_MILLISECONDS);

                    if (requestInfo.CancellationToken.IsCancellationRequested)
                    {
                        return(result.WithException(RequestCancelled));
                    }
                }
                catch (Exception e)
                {
                    LogNetworking("WazombiNetworking SendData exception at request.GetRequestStreamAsync: " + e);
                    return(result.WithException(ErrorCreatingStream));
                }

                try
                {
                    if (requestInfo.HasJson)
                    {
                        await dataStream.WriteAsync(byteArray, 0, byteArray.Length);

                        if (requestInfo.CancellationToken.IsCancellationRequested)
                        {
                            return(result.WithException(RequestCancelled));
                        }
                    }
                }
                catch (Exception e)
                {
                    LogNetworking("WazombiNetworking SendData exception at dataStream.WriteAsync: " + e);
                    return(result.WithException(ErrorWritingStream));
                }
                finally
                {
                    if (dataStream != null)
                    {
                        dataStream.Dispose();
                    }
                }
            }

            try
            {
                using (HttpWebResponse response = await request.GetResponseAsync(timeoutSpan, requestInfo.CancellationToken))
                {
                    if (requestInfo.CancellationToken.IsCancellationRequested)
                    {
                        return(result.WithException(RequestCancelled));
                    }

                    if (response == null)
                    {
                        LogNetworking("WazombiNetworking SendData response == null");
                        return(result.WithException(ErrorGettingResponse));
                    }

                    Stream stream      = response.GetResponseStream();
                    string returnValue = StreamToString(stream);

                    if (string.IsNullOrEmpty(returnValue))
                    {
                        return(result.WithStatus(response.StatusCode).WithCookies(response.Cookies));
                    }

                    try
                    {
                        JsonValue parsedJson = JsonValue.Parse(returnValue);
                        return(result.WithJson(parsedJson).WithStatus(response.StatusCode).WithCookies(response.Cookies));
                    }
                    catch (Exception e)
                    {
                        LogNetworking("WazombiNetworking SendData exception at JsonValue.Parse (returnValue): " + e);
                        return(result.WithException(ErrorParsingResponse));
                    }
                }
            }
            catch (Exception e)
            {
                LogNetworking("WazombiNetworking SendData exception at request.GetResponseAsync: " + e);
                return(result.WithException(ErrorGettingResponse));
            }
        }