Beispiel #1
0
        /// <summary>
        /// Get Http Request
        /// </summary>
        /// <param name="url">URL</param>
        /// <returns>String</returns>
        public async Task <string> HttpGetRequest(string url)
        {
            string result = String.Empty;

            try
            {
                var res = await httpClient.GetAsync(url);

                if (res.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    TokenExpired?.Invoke(this, null);
                }
                if (res.Content.Headers.ContentEncoding.Any((c) => c.ToLower().Contains("gzip")))
                {
                    using (var stream = new GZipStream(await res.Content.ReadAsStreamAsync(), CompressionMode.Decompress))
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            result = await reader.ReadToEndAsync();
                        }
                    }
                }
                else
                {
                    result = await res.Content.ReadAsStringAsync();
                }
            }
            catch
            {
            }
            return(result);
        }
Beispiel #2
0
        public async Task <string> HttpPutRequest(string url, string content, Dictionary <string, string> headers = null)
        {
            string result = string.Empty;

            try
            {
                HttpContent httpContent = new StringContent(content);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
                HttpResponseMessage httpResponseMessage = await httpClient.PutAsync(url, httpContent);

                if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    TokenExpired?.Invoke(this, null);
                }
                if (httpResponseMessage.Content.Headers.ContentEncoding.Any((c) => c.ToLower().Contains("gzip")))
                {
                    using (var stream = new GZipStream(await httpResponseMessage.Content.ReadAsStreamAsync(), CompressionMode.Decompress))
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            result = await reader.ReadToEndAsync();
                        }
                    }
                }
                else
                {
                    result = await httpResponseMessage.Content.ReadAsStringAsync();
                }
            }
            catch
            {
            }
            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Post Http Request
        /// </summary>
        /// <param name="url">Request Url</param>
        /// <param name="contentStr">Post Content</param>
        /// <returns></returns>
        public async Task <string> HttpPostRequest(string url, Stream content)
        {
            string result = string.Empty;

            try
            {
                var multi = new MultipartFormDataContent();
                //var dic = new Dictionary<string, string>();
                //var key = $"Content-Length:{content.Length}\r\nContent-Type:image/*\r\ncontent-disposition:form-data;name:\"picture\";filename:\"avatar.img\"";
                //dic.Add("Content-Length", content.Length.ToString());
                //dic.Add("Content-Type", "image/*");
                //dic.Add("content-disposition", "form-data");
                //dic.Add("name", "picture");
                //dic.Add("filename", "avatar.img");
                //multi.Add(new StringContent("content-disposition=form-data&name=picture&filename=avatar.img"));
                multi.Add(new StreamContent(content), Uri.EscapeUriString("content-disposition=form-data&name=\"picture\"&filename=\"avatar.img\""));

                HttpContent httpContent = multi;
                //httpClient.Headers.ContentType = new MediaTypeHeaderValue("image/*");
                //httpContent.Headers.ContentLength = content.Length;
                HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(url, httpContent);

                if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    TokenExpired?.Invoke(this, null);
                }
                if (httpResponseMessage.Content.Headers.ContentEncoding.Any((c) => c.ToLower().Contains("gzip")))
                {
                    using (var stream = new GZipStream(await httpResponseMessage.Content.ReadAsStreamAsync(), CompressionMode.Decompress))
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            result = await reader.ReadToEndAsync();
                        }
                    }
                }
                else
                {
                    result = await httpResponseMessage.Content.ReadAsStringAsync();
                }
            }
            catch
            {
            }
            return(result);
        }
Beispiel #4
0
        private bool ReceiveAuthToken()
        {
            // If the token expiration event is null, it will cause problems; we leave an exception for if this is the first time we are getting the data (add account screen)
            if (TokenExpired == null)
            {
                throw new ApiRequestException("[AniList] The token event handler has not been properly connected to handle it!");
            }
            var token = _credentials.Password;

            TokenExpired.Invoke(this, EventArgs.Empty);             // this should block the thread until the user is done entering the pin
            if (_credentials.Password == string.Empty)              // user decided not to update it!
            {
                _credentials.Password = token;
                return(false);
            }
            return(true);
        }
Beispiel #5
0
        /// <summary>
        /// Delete Http Request
        /// </summary>
        /// <param name="url"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public async Task <string> HttpDeleteRequest(string url, Dictionary <string, string> headers = null)
        {
            string result = string.Empty;

            try
            {
                HttpResponseMessage httpResponseMessage = await httpClient.DeleteAsync(url);

                if (httpResponseMessage != null && httpResponseMessage.Content != null)
                {
                    result = await httpResponseMessage.Content.ReadAsStringAsync();
                }
                if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    TokenExpired?.Invoke(this, null);
                }
                if (httpResponseMessage.Content.Headers.ContentEncoding.Any((c) => c.ToLower().Contains("gzip")))
                {
                    using (var stream = new GZipStream(await httpResponseMessage.Content.ReadAsStreamAsync(), CompressionMode.Decompress))
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            result = await reader.ReadToEndAsync();
                        }
                    }
                }
                else
                {
                    result = await httpResponseMessage.Content.ReadAsStringAsync();
                }
            }
            catch
            {
            }
            return(result);
        }
 private void TokenExpiredHandler(object sender, EventArgs e)
 {
     TokenExpired?.Invoke(this, EventArgs.Empty);
 }