Example #1
0
        private static DownloadContent ProcessResponse(HttpWebResponse httpWebResponse)
        {
            using (var responseStream = httpWebResponse.GetResponseStream())
            {
                DownloadContent downcontent  = new DownloadContent();
                MemoryStream    memorystream = new MemoryStream();
                responseStream.CopyTo(memorystream);
                byte[] databytes = memorystream.ToArray();

                if (httpWebResponse.StatusCode != HttpStatusCode.OK && httpWebResponse.StatusCode != HttpStatusCode.Moved && httpWebResponse.StatusCode != HttpStatusCode.Found)
                {
                    downcontent.StatusCode = 0;
                    return(null);
                }
                else
                {
                    downcontent.StatusCode = 200;
                }

                var contentType = httpWebResponse.Headers["content-type"];

                if (!string.IsNullOrEmpty(contentType))
                {
                    downcontent.ContentType = contentType.ToLower();
                }

                if (string.IsNullOrEmpty(downcontent.ContentType) || IOHelper.IsStringType(downcontent.ContentType))
                {
                    downcontent.isString = true;

                    var encoding = EncodingDetector.GetEncoding(ref databytes, contentType);
                    if (encoding != null)
                    {
                        downcontent.ContentString = encoding.GetString(databytes);
                        downcontent.Encoding      = encoding.WebName;
                    }
                }
                else
                {
                    downcontent.DataBytes = databytes;
                    downcontent.isString  = false;
                }

                return(downcontent);
            }
        }
Example #2
0
        private static async Task <DownloadContent> ProcessResponse1(HttpResponseMessage response)
        {
            byte[] databytes = await response.Content.ReadAsByteArrayAsync();

            DownloadContent downcontent = new DownloadContent();

            downcontent.ResponseHeader = response.Headers;

            if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Moved && response.StatusCode != HttpStatusCode.Found)
            {
                downcontent.StatusCode = 0;
                return(null);
            }
            else
            {
                downcontent.StatusCode = 200;
            }

            var contentType = response.Content.Headers.ContentType?.ToString();

            if (!string.IsNullOrEmpty(contentType))
            {
                downcontent.ContentType = contentType.ToLower();
            }

            downcontent.DataBytes = databytes;

            if (string.IsNullOrEmpty(downcontent.ContentType) || IOHelper.IsStringType(downcontent.ContentType))
            {
                downcontent.isString = true;

                var encoding = EncodingDetector.GetEncoding(ref databytes, contentType);
                if (encoding != null)
                {
                    downcontent.ContentString = encoding.GetString(databytes);
                    downcontent.Encoding      = encoding.WebName;
                }
            }
            else
            {
                downcontent.isString = false;
            }

            return(downcontent);
        }