Esempio n. 1
0
        private void CopyHeaders(HttpRequestInfo requestInfo, HttpWebRequest webRequest)
        {
            //by default set 100Continue to false to prevent 417 errors
            //however if the client sends an expect header we will change this back
            webRequest.ServicePoint.Expect100Continue = false;

            //add headers
            foreach (HTTPHeader header in requestInfo.Headers)
            {
                if (String.Compare(header.Name, "Accept", true) == 0)
                {
                    webRequest.Accept = header.Value;
                }
                else if (String.Compare(header.Name, "Connection", true) == 0 || String.Compare(header.Name, "Proxy-Connection", true) == 0)
                {
                    if (String.Compare(header.Value, "close", true) == 0)
                    {
                        webRequest.KeepAlive = false;
                    }
                    else
                    {
                        webRequest.KeepAlive = true;
                    }
                }
                else if (String.Compare(header.Name, "Content-Length", true) == 0)
                {
                    long length;
                    if (long.TryParse(header.Value, out length))
                    {
                        webRequest.ContentLength = length;
                    }
                }
                else if (String.Compare(header.Name, "Content-Type", true) == 0)
                {
                    webRequest.ContentType = header.Value;
                }
                else if (String.Compare(header.Name, "If-Modified-Since", true) == 0)
                {
                    DateTime date;
                    if (DateTime.TryParse(header.Value, out date))
                    {
                        webRequest.IfModifiedSince = date;
                    }
                }
                else if (String.Compare(header.Name, "Referer", true) == 0)
                {
                    webRequest.Referer = header.Value;
                }
                else if (String.Compare(header.Name, "Transfer-Encoding", true) == 0)
                {
                    webRequest.TransferEncoding = header.Value;
                }
                else if (String.Compare(header.Name, "User-Agent", true) == 0)
                {
                    webRequest.UserAgent = header.Value;
                }
                else if (String.Compare(header.Name, "Date", true) == 0)
                {
                    DateTime date;
                    if (DateTime.TryParse(header.Value, out date))
                    {
                        webRequest.Date = date;
                    }
                    else
                    {
                        //the date could not be parsed but try to set now as the date
                        webRequest.Date = DateTime.Now;
                    }
                }
                else if (String.Compare(header.Name, "Range", true) == 0)
                {
                    string specifier;
                    int    from;
                    int    to;

                    HttpUtil.ParseRange(header.Value, out specifier, out from, out to);
                    if (from < to)
                    {
                        webRequest.AddRange(specifier, from, to);
                    }
                }
                else if (String.Compare(header.Name, "Expect", true) == 0)
                {
                    if (String.Compare(header.Value, "100-Continue", true) == 0)
                    {
                        webRequest.ServicePoint.Expect100Continue = true;
                    }
                }
                else if (String.Compare(header.Name, "Host", true) == 0)
                {
                    //do nothing for these headers
                    ;
                }
                else if (String.Compare(header.Name, "Cookie", true) == 0 && ShouldHandleCookies)
                {
                    //add the additional cookies to the jar
                    //if should handle cookies is on cookie management is done by the jar (we want to be able to add cookies)
                    HttpVariables vars = new HttpVariables(header.Value, RequestLocation.Cookies);
                    foreach (var cookie in vars)
                    {
                        _cookieJar.Add(new Cookie(cookie.Key, cookie.Value, requestInfo.Path, requestInfo.Host));
                    }
                }
                else
                {
                    try
                    {
                        //webRequest doesn't support PseudoHeaders
                        if (!header.Name.StartsWith(":"))
                        {
                            webRequest.Headers.Set(header.Name, header.Value);
                        }
                    }
                    catch
                    {
                        SdkSettings.Instance.Logger.Log(TraceLevel.Error, "WebRequestClient error. Cannot set header {0}", header.Name);
                    }
                }
            }
        }