Пример #1
0
        protected void BasePublish <T>(string exchangeType, string routingKey, params T[] msgs)
        {
            var channel = channelPool.Rent();

            try
            {
                if (!string.IsNullOrEmpty(__exchangeName))
                {
                    channel.ExchangeDeclare(__exchangeName, exchangeType, false, false, null);
                }
                foreach (var msg in msgs)
                {
                    var sendBytes = Encoding.UTF8.GetBytes(msg.ToJson());
                    channel.BasicPublish(__exchangeName, routingKey, __basicProperties, sendBytes);
                }
            }
            catch (Exception ero)
            {
                throw ero;
            }
            finally
            {
                channelPool.Return(channel);
            }
        }
Пример #2
0
        public async Task <string> SendDataAsync(string url, string method, string data)
        {
            //httpclient的问题
            //https://www.cnblogs.com/jlion/p/12813692.html
            var httpClient = httpClientPool.Rent();

            httpClient.DefaultRequestHeaders.Clear();
            //httpClient.BaseAddress = new Uri(url);
            httpClient.DefaultRequestHeaders.Add("ContentType", ContentType);
            httpClient.DefaultRequestHeaders.Add("Accept", Accept);
            foreach (var kv in heads)
            {
                httpClient.DefaultRequestHeaders.Add(kv.Key, kv.Value.ToString());
            }
            var content = new StringContent(data, ContentEncoding);

            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(ContentType);
            HttpResponseMessage response;

            switch (method)
            {
            case "POST":
                response = await httpClient.PostAsync(url, content);

                break;

            case "PUT":
                response = await httpClient.PutAsync(url, content);

                break;

            case "DELETE":
                response = await httpClient.DeleteAsync(url);

                break;

            default:
                response = await httpClient.GetAsync(url);

                break;
            }
            httpClientPool.Return(httpClient);
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception("服务器错误:" + response.StatusCode);
            }
            string result;

            using (var myResponseStream = await response.Content.ReadAsStreamAsync())
            {
                using (var myStreamReader = new StreamReader(myResponseStream, ResponseEncoding))
                {
                    result = myStreamReader.ReadToEnd();
                }
            }
            //httpClientPool.Return(httpClient);
            return(result);
        }
Пример #3
0
 public IPixelBuffer Rent() => _pool.Rent();
Пример #4
0
        public async Task <HttpResponseResult> SendDataAsyncBase(string url, string method, string data)
        {
            //httpclient的问题
            //https://www.cnblogs.com/jlion/p/12813692.html
            HttpClient httpClient = _httpClient;

            if (_httpClient == null)
            {
                httpClient = httpClientPool.Rent();
            }
            httpClient.DefaultRequestHeaders.Clear();
            //httpClient.BaseAddress = new Uri(url);
            httpClient.DefaultRequestHeaders.Add("ContentType", ContentType);
            httpClient.DefaultRequestHeaders.Add("Accept", Accept);
            foreach (var kv in heads)
            {
                httpClient.DefaultRequestHeaders.Add(kv.Key, kv.Value.ToString());
            }
            if (RequestWidthCookie)
            {
                var cookies = new List <string>();
                foreach (Cookie c in GetCurrentCookie())
                {
                    var str = $"{c.Name}={c.Value}";
                    cookies.Add(str);
                }
                httpClient.DefaultRequestHeaders.Add("Cookie", string.Join("&", cookies));
            }
            var content = new StringContent(data, ContentEncoding);

            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(ContentType);
            HttpResponseMessage response;

            switch (method)
            {
            case "POST":
                response = await httpClient.PostAsync(url, content);

                break;

            case "PUT":
                response = await httpClient.PutAsync(url, content);

                break;

            case "DELETE":
                response = await httpClient.DeleteAsync(url);

                break;

            default:
                response = await httpClient.GetAsync(url);

                break;
            }
            if (_httpClient == null)
            {
                httpClientPool.Return(httpClient);
            }
            var stream = await response.Content.ReadAsStreamAsync();

            return(new HttpResponseResult(stream, response.StatusCode));
        }