Ejemplo n.º 1
0
            private async Task <byte[]> getResponseData(CancellationToken?cancellationToken)
            {
                if (cancellationToken == null)
                {
                    cancellationToken = new CancellationToken();
                }

                //var client = new HttpClient(_handler, true);
                var client = _httpClientFactory.CreateClient("defaulthttpgetter");

                client.BaseAddress = _requestMsg.RequestUri;

                var contentType = _contentType;

                if (_requestMsg.Method == HttpMethod.Post)
                {
                    if (_jsonContent != null)
                    {
                        if (!_hasChangeContentType)
                        {
                            contentType = ContentTypeEnum.Json;
                        }

                        var content = new StringContent(_jsonContent.ToStringEx(Formatting.None), _encoding, getContentType(contentType));

                        _requestMsg.Content = content;
                    }
                    else if (_values.HasData())
                    {
                        var hasFile = _values.Any(x => x.Value is FormFile);

                        if (!_hasChangeContentType)
                        {
                            if (hasFile)
                            {
                                contentType = ContentTypeEnum.FormData;
                            }
                            else
                            {
                                contentType = ContentTypeEnum.FormUrlencoded;
                            }
                        }

                        if (!hasFile && contentType != ContentTypeEnum.FormData)
                        {
                            var content = new FormUrlEncodedContent(_values.Select(x => new KeyValuePair <string, string>(x.Key, x.Value.ToStringEx())));

                            _requestMsg.Content = content;
                        }
                        else
                        {
                            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
                            var    content  = new MultipartFormDataContent(boundary);

                            foreach (var value in _values)
                            {
                                if (value.Value is FormFile)
                                {
                                    var file = (FormFile)value.Value;

                                    if (!file.FileData.HasData())
                                    {
                                        continue;
                                    }

                                    content.Add(new ByteArrayContent(file.FileData), value.Key, file.FilePath);
                                }
                                else
                                {
                                    content.Add(new StringContent((string)value.Value), value.Key);
                                }
                            }
                        }
                    }
                    else
                    {
                        var content = new StringContent(_stringContent, _encoding, getContentType(contentType));

                        _requestMsg.Content = content;
                    }
                }



                if (_cookies.IsValueCreated && _cookies.Value.HasData())
                {
                    var cookie = _cookies.Value.Select(x => $"{x.Key}={x.Value};").JoinToString(" ");

                    _requestMsg.Headers.Add("Cookie", cookie);
                }

                HttpResponseMessage resp = null;

retry:

                try
                {
                    //if (cancellationToken.HasValue)
                    //{
                    resp = (await client.SendAsync(_requestMsg, cancellationToken.Value)).EnsureSuccessStatusCode();
                    //}

                    var sourceStream = await resp.Content.ReadAsStreamAsync();

                    byte[] data = null;

                    if (resp.Content.Headers.ContentEncoding.Any(x => x.Contains("gzip")))
                    {
                        using (GZipStream stream = new GZipStream(sourceStream, CompressionMode.Decompress))
                        {
                            data = await stream.ReadAllBytesAsync();
                        }
                    }
                    else if (resp.Content.Headers.ContentEncoding.Any(x => x.Contains("deflate")))
                    {
                        using (DeflateStream stream = new DeflateStream(sourceStream, CompressionMode.Decompress))
                        {
                            data = await stream.ReadAllBytesAsync();
                        }
                    }
                    else
                    {
                        data = await sourceStream.ReadAllBytesAsync();
                    }

                    _retryCount--;

                    return(data);
                }
                catch (Exception e)
                {
                    var content = "";

                    if (resp == null)
                    {
                        content = await resp.Content.ReadAsStringAsync();
                    }

                    LoggerManager.Default.Debug("读取错误:" + (string.IsNullOrWhiteSpace(content)?e.Message:content));
                    _retryCount--;

                    if (_retryCount > 0)
                    {
                        goto retry;
                    }

                    throw new HttpWebGetterException(_requestMsg.RequestUri.ToStringEx(), e, content);
                }
            }