Exemple #1
0
        public async Task <Tuple <Stream, string> > DownAsync(string url, string referer, bool isReferer)
        {
            try
            {
                string fileName = "";
                int    count    = 0;
                while (count <= 3)
                {
                    var client = _clientFactory.CreateClient("gzip");
                    if (referer.IsNotEmpty())
                    {
                        client.DefaultRequestHeaders.Add("referer", referer);
                    }
                    var responseMessage = await client.GetAsync(url);

                    if (responseMessage.StatusCode == HttpStatusCode.OK)
                    {
                        fileName = $"image.{MimeUtility.GetExtensionByMime(responseMessage.Content.Headers.ContentType.MediaType)}";
                        //fileName = Path.GetFileName(url);
                        var stream = await responseMessage.Content.ReadAsStreamAsync();

                        return(new Tuple <Stream, string>(stream, fileName));
                    }
                    count += 1;
                }

                return(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "下载文件错误:" + url);
                return(null);
            }
        }