/// <summary> /// removes all headers with given name /// </summary> /// <param name="headerName"></param> /// <returns>True if header was removed /// False if no header exists with given name</returns> public bool RemoveHeader(string headerName) { if (RequestHeaders.ContainsKey(headerName)) { RequestHeaders.Remove(headerName); return(true); } else if (NonUniqueRequestHeaders.ContainsKey(headerName)) { NonUniqueRequestHeaders.Remove(headerName); return(true); } return(false); }
private HttpWebRequest CreateRequest(string method) { var request = (HttpWebRequest)WebRequest.Create(CurrentUrl); request.Method = method.ToUpper(); request.CookieContainer = _cookies; request.UserAgent = _userAgent; request.AllowAutoRedirect = true; request.MaximumAutomaticRedirections = MaxRedirects; if (_urlReferrer != null) { request.Referer = _urlReferrer.AbsoluteUri; } if (RequestHeaders != null) { // Use the appropriate property for some headers. var accept = RequestHeaders["Accept"]; if (accept != null) { RequestHeaders.Remove("Accept"); } request.Headers = RequestHeaders; if (accept != null) { request.Accept = accept; } // Once used don't use them again. RequestHeaders = null; } // Never time out when debugging. if (Debugger.IsAttached) { request.Timeout = Timeout.Infinite; } return(request); }
/// <summary> /// Removes given header object if it exist /// </summary> /// <param name="header">Returns true if header exists and was removed </param> public bool RemoveHeader(HttpHeader header) { if (RequestHeaders.ContainsKey(header.Name)) { if (RequestHeaders[header.Name].Equals(header)) { RequestHeaders.Remove(header.Name); return(true); } } else if (NonUniqueRequestHeaders.ContainsKey(header.Name)) { if (NonUniqueRequestHeaders[header.Name] .RemoveAll(x => x.Equals(header)) > 0) { return(true); } } return(false); }
/// <summary> /// Adds the given header object to Request /// </summary> /// <param name="newHeader"></param> public void AddHeader(HttpHeader newHeader) { if (NonUniqueRequestHeaders.ContainsKey(newHeader.Name)) { NonUniqueRequestHeaders[newHeader.Name].Add(newHeader); return; } if (RequestHeaders.ContainsKey(newHeader.Name)) { var existing = RequestHeaders[newHeader.Name]; RequestHeaders.Remove(newHeader.Name); NonUniqueRequestHeaders.Add(newHeader.Name, new List <HttpHeader>() { existing, newHeader }); } else { RequestHeaders.Add(newHeader.Name, newHeader); } }