Beispiel #1
0
        /// <summary>
        /// Factory for creating the HttpWebRequest.
        /// </summary>
        /// <param name="httpMethod"></param>
        /// <param name="resource"></param>
        /// <returns>HttpWebRequest</returns>
        protected virtual HttpWebRequest CreateHttpWebRequest()
        {
            var webRequest = (HttpWebRequest)System.Net.WebRequest.Create(RestUri.ToString());

            webRequest.AllowAutoRedirect      = AllowAutoRedirect;
            webRequest.AuthenticationLevel    = AuthenticationLevel;
            webRequest.AutomaticDecompression = AutomaticDecompression;
            webRequest.ContentType            = ContentType.ToString();
            webRequest.CachePolicy            = CachePolicy;
            webRequest.CookieContainer        = CookieContainer;
            webRequest.Credentials            = Credentials;
            webRequest.KeepAlive = KeepAlive;
            webRequest.MaximumAutomaticRedirections = MaximumAutomaticRedirections;
            webRequest.Method    = Method.ToString();
            webRequest.Referer   = Referer;
            webRequest.UserAgent = UserAgent;

            foreach (var cert in ClientCertificates)
            {
                webRequest.ClientCertificates.Add(cert);
            }

            if (TimeOut != TimeSpan.MinValue && TimeOut > TimeSpan.FromSeconds(0))
            {
                webRequest.ReadWriteTimeout = (int)TimeOut.TotalMilliseconds;
                webRequest.Timeout          = (int)TimeOut.TotalMilliseconds;
            }

            if (TcpKeepAlive)
            {
                webRequest.ServicePoint.SetTcpKeepAlive(true, (int)TcpKeepAliveTimeOut.TotalMilliseconds, 1000);
            }

            foreach (var key in Headers.AllKeys)
            {
                if (WebHeaderCollection.IsRestricted(key))
                {
                    SetRestrictedHeaderProperty(webRequest, key, Headers[key]);
                }
                else
                {
                    webRequest.Headers.Add(key, Headers[key]);
                }
            }

            OnWebRequestCreated(webRequest);

            return(webRequest);
        }
Beispiel #2
0
        public RestRequest(HttpMethod method, RestUri uri)
        {
            RestUri = uri;
            Headers = new WebHeaderCollection();

            AllowAutoRedirect            = true;
            AuthenticationLevel          = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
            AutomaticDecompression       = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            CachePolicy                  = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
            ClientCertificates           = new System.Security.Cryptography.X509Certificates.X509CertificateCollection();
            CookieContainer              = new System.Net.CookieContainer();
            ContentType                  = ContentType.ApplicationX_WWW_Form_UrlEncoded;
            MaximumAutomaticRedirections = 50;
            TimeOut             = TimeSpan.FromMinutes(6);
            TcpKeepAliveTimeOut = TimeSpan.FromHours(2);
            UserAgent           = "Resty.Net-RestRequest";
            Method = method;
        }