Exemplo n.º 1
0
        private void AddByAppendType(HttpRequestHeaders headers, string key, string value)
        {
            switch (_appendHeaderType)
            {
            case AppendHeaderType.Add:
                headers.TryAddWithoutValidation(key, value);
                break;

            case AppendHeaderType.AddIfNotExist:
                if (!headers.Contains(key))
                {
                    headers.TryAddWithoutValidation(key, value);
                }
                break;

            case AppendHeaderType.AddOrReplace:
                if (headers.Contains(key))
                {
                    headers.Remove(key);
                }
                headers.TryAddWithoutValidation(key, value);
                break;

            default:
                break;
            }
        }
 /// <summary>
 /// Adds a header to the <see cref="HttpRequestHeaders"/> list object.
 /// </summary>
 /// <param name="headers">The request headers list object.</param>
 /// <param name="name">The name of the header to add.</param>
 /// <param name="value">The value of the header.</param>
 private static void AddHeader(HttpRequestHeaders headers, string name, string value)
 {
     if (headers.Contains(name))
     {
         headers.Remove(name);
     }
     headers.TryAddWithoutValidation(name, value);
 }
 public static void SetWithoutValidation(this HttpRequestHeaders headers, string name, string value)
 {
     if (headers.Contains(name))
     {
         headers.Remove(name);
     }
     headers.TryAddWithoutValidation(name, value);
 }
Exemplo n.º 4
0
        private void SetAuthorizationHeader(HttpRequestHeaders defaultRequestHeaders)
        {
            defaultRequestHeaders.Remove("Authorization");

            defaultRequestHeaders.Add(
                "Authorization",
                $"Bearer {accessToken}");
        }
Exemplo n.º 5
0
 private void setSessionHeaders(HttpRequestHeaders headers)
 {
     if (headers.Contains("Cookie"))
     {
         headers.Remove("Cookie");
     }
     headers.Add("Cookie", $"PHPSESSID={secureStringToString(sessionId)}");
 }
Exemplo n.º 6
0
 /// <summary>
 /// Handy workaround for setting/overriding header properties.
 /// Microsoft does not make it easy.
 /// </summary>
 /// <param name="headers">Header object</param>
 /// <param name="name">Name of header property</param>
 /// <param name="value">Value to set</param>
 internal static void Set(this HttpRequestHeaders headers, string name, string value)
 {
     if (headers.Contains(name))
     {
         headers.Remove(name);
     }
     headers.Add(name, value);
 }
Exemplo n.º 7
0
 private void CopyRequestHeaders(HttpRequestHeaders dst, HttpRequestHeaders src)
 {
     if (src != null)
     {
         foreach (KeyValuePair <string, IEnumerable <string> > header in src)
         {
             dst.Remove(header.Key);
             dst.Add(header.Key, header.Value);
         }
     }
 }
Exemplo n.º 8
0
        /// <inheritdoc />
        public void SetClientRequestId(Guid guid)
        {
            HttpRequestHeaders headers = HttpClient.DefaultRequestHeaders;

            if (headers.Contains(ClientRequestIdHeaderName))
            {
                headers.Remove(ClientRequestIdHeaderName);
            }

            headers.Add(ClientRequestIdHeaderName, guid.ToString());
        }
 public static void add(this HttpRequestHeaders headers, string name, string value)
 {
     if (!headers.Contains(name))
     {
         headers.Add(name, value);
     }
     else
     {
         headers.Remove(name);
         headers.Add(name, value);
     }
 }
Exemplo n.º 10
0
        private static void SetHttpRequestHeader(HttpRequestHeaders header, string idToThrottle, string correlationId)
        {
            if (header != null)
            {
                lock (header)
                {
                    header.Remove(AzPredictorService.ThrottleByIdHeader);

                    if (!string.IsNullOrWhiteSpace(idToThrottle))
                    {
                        header.Add(AzPredictorService.ThrottleByIdHeader, idToThrottle);
                    }

                    header.Remove(AzPredictorService.CorrelationIdHeader);

                    if (!string.IsNullOrWhiteSpace(correlationId))
                    {
                        header.Add(AzPredictorService.CorrelationIdHeader, correlationId);
                    }
                }
            }
        }
        public static void Set(this HttpRequestHeaders headers, string name, string value)
        {
            if (headers == null)
            {
                throw new ArgumentNullException(nameof(headers));
            }

            if (headers.Contains(name))
            {
                headers.Remove(name);
            }
            headers.Add(name, value);
        }
        /// <summary>
        /// Adds a header name/value if the header name does not exist; otherwise replaces the
        /// existing value.
        /// </summary>
        /// <param name="source">HTTP request headers.</param>
        /// <param name="name">Header name.</param>
        /// <param name="value">Header value.</param>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception>
        public static void AddOrUpdate(this HttpRequestHeaders source, string name, string value)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (source.Contains(name))
            {
                source.Remove(name);
            }

            source.Add(name, value);
        }
        private static void ReplaceThrottleUserIdToHeader(HttpRequestHeaders header, string value)
        {
            if (header != null)
            {
                lock (header)
                {
                    header.Remove(AzPredictorService.ThrottleByIdHeader);

                    if (!string.IsNullOrWhiteSpace(value))
                    {
                        header.Add(AzPredictorService.ThrottleByIdHeader, value);
                    }
                }
            }
        }
Exemplo n.º 14
0
 /// <summary>
 /// 设置HttpClient的Headers值,如存在,则先移除
 /// </summary>
 public static void Set(this HttpRequestHeaders headers, string name, string value)
 {
     if (headers.Contains(name))
     {
         headers.Remove(name);
     }
     try
     {
         headers.Add(name, value);
     }
     catch (FormatException)
     {
         headers.TryAddWithoutValidation(name, value);
     }
 }
Exemplo n.º 15
0
 /// <summary>
 /// 更新头信息
 /// </summary>
 /// <param name="headers"></param>
 /// <param name="headerdic"></param>
 internal static void UpdateRequestHeader(HttpRequestHeaders headers, Dictionary <string, string> headerdic)
 {
     if (headerdic == null)
     {
         return;
     }
     foreach (var kv in headerdic)
     {
         if (headers.Contains(kv.Key))
         {
             headers.Remove(kv.Key);
         }
         headers.Add(kv.Key, kv.Value);
     }
 }
Exemplo n.º 16
0
        public static void AssignHeaders(HttpRequestHeaders headers, IdPayGatewayAccount account)
        {
            var api = account.IsTestAccount ? SandBoxApiKey : account.Api;

            headers.AddOrUpdate(ApiKey, api);

            if (account.IsTestAccount)
            {
                headers.AddOrUpdate(SandBoxKey, "1");
            }
            else if (headers.Contains(SandBoxKey))
            {
                headers.Remove(SandBoxKey);
            }
        }
Exemplo n.º 17
0
        private static void AddHeaders(ref HttpRequestHeaders httpHeaders, IDictionary <string, string> headerDic, bool addDefaultHeaders = true)
        {
            if (httpHeaders == null)
            {
                return;
            }

            var dic = new Dictionary <string, string>();

            if (addDefaultHeaders && _defaultHeaders != null && _defaultHeaders.Count > 0)
            {
                foreach (var defaultHeader in _defaultHeaders)
                {
                    dic.Add(defaultHeader.Key, defaultHeader.Value);
                }
            }
            if (headerDic != null && headerDic.Count > 0)
            {
                foreach (var header in headerDic)
                {
                    var name  = header.Key;
                    var value = header.Value;
                    if (dic.ContainsKey(name))
                    {
                        dic.Remove(name);
                    }
                    dic.Add(name, value);
                }
            }

            if (dic.Count > 0)
            {
                foreach (var item in dic)
                {
                    var name  = item.Key;
                    var value = item.Value;
                    if (httpHeaders.Contains(name))
                    {
                        httpHeaders.Remove(name);
                    }
                    httpHeaders.Add(name, value);
                }
            }
        }
Exemplo n.º 18
0
        public void RemoveTakesHeadersOutOfDictionary()
        {
            IDictionary <string, StringValues> headers = new HttpRequestHeaders();

            headers.Add("host", new[] { "localhost" });
            headers.Add("custom", new[] { "value" });
            headers.Add("Content-Length", new[] { "0" });

            StringValues value;

            Assert.Equal(3, headers.Count);
            Assert.True(headers.TryGetValue("host", out value));
            Assert.True(headers.TryGetValue("custom", out value));
            Assert.True(headers.TryGetValue("Content-Length", out value));

            Assert.True(headers.Remove("host"));
            Assert.False(headers.Remove("host"));

            Assert.Equal(2, headers.Count);
            Assert.False(headers.TryGetValue("host", out value));
            Assert.True(headers.TryGetValue("custom", out value));

            Assert.True(headers.Remove("custom"));
            Assert.False(headers.Remove("custom"));

            Assert.Equal(1, headers.Count);
            Assert.False(headers.TryGetValue("host", out value));
            Assert.False(headers.TryGetValue("custom", out value));
            Assert.True(headers.TryGetValue("Content-Length", out value));

            Assert.True(headers.Remove("Content-Length"));
            Assert.False(headers.Remove("Content-Length"));

            Assert.Equal(0, headers.Count);
            Assert.False(headers.TryGetValue("host", out value));
            Assert.False(headers.TryGetValue("custom", out value));
            Assert.False(headers.TryGetValue("Content-Length", out value));
        }
Exemplo n.º 19
0
        public static bool RemoveIdempotencyHeader(this HttpRequestHeaders @this)
        {
            @this.ThrowIfNull(typeof(HttpRequestHeaders).Name.ToLower());

            return(@this.Remove(IdempotencyHeader));
        }
Exemplo n.º 20
0
        public void Header_BaseImplementation()
        {
            HttpRequestMessage message = new HttpRequestMessage();
            HttpRequestHeaders headers = message.Headers;

            headers.Add("a", "a-value");
            headers.Add("b", new List <string> {
                "v1", "v2"
            });
            headers.Add("c", null as string);
            headers.Add("d", new string[0]);

            Assert.IsTrue(headers.TryAddWithoutValidation("accept", "audio"), "#0");

            Assert.IsFalse(headers.Contains("nn"), "#1a");
            Assert.IsTrue(headers.Contains("b"), "#1b");

            var values = headers.GetValues("b").ToList();

            Assert.AreEqual("v1", values[0], "#2a");
            Assert.AreEqual("v2", values[1], "#2b");

            Assert.IsFalse(headers.Remove("p"), "#3a");
            Assert.IsTrue(headers.Remove("b"), "#3b");
            Assert.IsFalse(headers.Contains("b"), "#3b-c");

            IEnumerable <string> values2;

            Assert.IsTrue(headers.TryGetValues("c", out values2));
            values = values2.ToList();
            Assert.AreEqual("", values[0], "#4a");

            int counter = 0;

            foreach (var i in headers)
            {
                ++counter;
            }

            Assert.AreEqual(3, counter, "#5");

            headers.Clear();

            headers.Accept.Add(new MediaTypeWithQualityHeaderValue("audio/x"));
            Assert.IsTrue(headers.TryAddWithoutValidation("accept", "audio"), "#55");

            values = headers.GetValues("accept").ToList();
            Assert.AreEqual(2, values.Count, "#6");
            Assert.AreEqual("audio/x", values[0], "#6a");
            Assert.AreEqual("audio", values[1], "#6b");
            Assert.AreEqual(1, headers.Accept.Count, "#6c");

            headers.Clear();

            Assert.IsTrue(headers.TryAddWithoutValidation("from", new[] { "*****@*****.**", "*****@*****.**" }), "#70");
            values = headers.GetValues("from").ToList();

            Assert.AreEqual(2, values.Count, "#7");
            Assert.AreEqual("*****@*****.**", values[0], "#7a");
            Assert.AreEqual("*****@*****.**", values[1], "#7b");
            Assert.AreEqual("*****@*****.**", headers.From, "#7c");

            headers.Clear();

            Assert.IsTrue(headers.TryAddWithoutValidation("Date", "wrong date"), "#8-0");
            var value = headers.Date;

            Assert.IsNull(headers.Date, "#8");
        }
Exemplo n.º 21
0
 public static HttpRequestHeaders SetLoose(this HttpRequestHeaders headers, string key, string value)
 {
     headers.Remove(key);
     headers.TryAddWithoutValidation(key, value);
     return(headers);
 }
Exemplo n.º 22
0
 public static HttpRequestHeaders Set(this HttpRequestHeaders headers, string key, string value)
 {
     headers.Remove(key);
     headers.Add(key, value);
     return(headers);
 }
Exemplo n.º 23
0
 public override void Remove(string key)
 {
     _headers.Remove(key);
 }