Ejemplo n.º 1
0
        public static String Download(Request requestParams)
        {
            try
            {
                var request  = makeRequest(requestParams);
                var responce = (HttpWebResponse)request.GetResponse();
                if ((int)responce.StatusCode < 200 || (int)responce.StatusCode >= 300)
                {
                    Log.Add(RecType.Error, "Download url: " + requestParams.Url + "; Responce Code = " + (int)responce.StatusCode);
                    return(null);
                }
                using (var stream = responce.GetResponseStream())
                {
                    if (stream == null)
                    {
                        Log.Add(RecType.Error, "Download url: " + requestParams.Url + "; stream == null");
                        return(null);
                    }
                    var uncompressedStream = stream;
                    switch (responce.ContentEncoding.ToLower())
                    {
                    case "gzip":
                        uncompressedStream = new MemoryStream();
                        CompressionUtils.UnPack(stream, uncompressedStream, CompressMethod.TextGZip);
                        uncompressedStream.Position = 0;
                        break;

                    case "deflate":
                        uncompressedStream = new MemoryStream();
                        CompressionUtils.UnPack(stream, uncompressedStream, CompressMethod.TextDeflate);
                        uncompressedStream.Position = 0;
                        break;
                    }
                    var reader  = new StreamReader(uncompressedStream, requestParams.Encoding, true);
                    var content = reader.ReadToEnd();
                    return(content);
                }
            }
            catch (Exception exception)
            {
                Log.Add(RecType.Error, "Download url: " + requestParams.Url, exception);
                return(null);
            }
        }