Esempio n. 1
0
        private void PreparePostBody(WebRequest webRequest)
        {
            bool needsContentType = string.IsNullOrEmpty(webRequest.ContentType);

            if (HasFiles || AlwaysMultipartFormData)
            {
                if (needsContentType)
                {
                    webRequest.ContentType = GetMultipartFormContentType();
                }
                else if (!webRequest.ContentType.Contains("boundary"))
                {
                    webRequest.ContentType = webRequest.ContentType + "; boundary=" + FormBoundary;
                }
            }
            else if (HasBody)
            {
                if (needsContentType)
                {
                    webRequest.ContentType = RequestContentType;
                }
            }
            else if (HasParameters)
            {
                if (needsContentType)
                {
                    webRequest.ContentType = "application/x-www-form-urlencoded";
                }
                RequestBody = EncodeParameters();
            }
        }
Esempio n. 2
0
 protected virtual HttpWebRequest CreateWebRequest(Uri url)
 {
     return((HttpWebRequest)WebRequest.Create(url));
 }
        private IHttp ConfigureHttp(IRestRequest request)
        {
            var http = new Http
            {
                Encoding = Encoding,
                AlwaysMultipartFormData = request.AlwaysMultipartFormData,
                UseDefaultCredentials   = request.UseDefaultCredentials,
                ResponseWriter          = request.ResponseWriter,
                AdvancedResponseWriter  = request.AdvancedResponseWriter,
                CookieContainer         = CookieContainer,
                AutomaticDecompression  = AutomaticDecompression,
                WebRequestConfigurator  = WebRequestConfigurator,
                Encode = Encode
            };

            var requestParameters = new List <Parameter> ();

            requestParameters.AddRange(request.Parameters);

            // move RestClient.DefaultParameters into Request.Parameters
            foreach (var defaultParameter in DefaultParameters)
            {
                var parameterExists =
                    request.Parameters.Any(p =>
                                           p.Name.Equals(defaultParameter.Name, StringComparison.InvariantCultureIgnoreCase) &&
                                           p.Type == defaultParameter.Type);

                if (AllowMultipleDefaultParametersWithSameName)
                {
                    var isMultiParameter = MultiParameterTypes.Any(pt => pt == defaultParameter.Type);
                    parameterExists = !isMultiParameter && parameterExists;
                }

                if (!parameterExists)
                {
                    requestParameters.Add(defaultParameter);
                }
            }

            // Add Accept header based on registered deserializers if none has been set by the caller.
            if (requestParameters.All(
                    p => !string.Equals(p.Name, "accept", StringComparison.InvariantCultureIgnoreCase)))
            {
                var accepts = string.Join(", ", AcceptTypes.ToArray());

                requestParameters.Add(new Parameter("Accept", accepts, ParameterType.HttpHeader));
            }

            http.Url             = BuildUri(request);
            http.Host            = BaseHost;
            http.PreAuthenticate = PreAuthenticate;
            http.UnsafeAuthenticatedConnectionSharing = UnsafeAuthenticatedConnectionSharing;

            var userAgent = UserAgent ?? http.UserAgent;

            http.UserAgent = userAgent.HasValue()
                                ? userAgent
                                : "RestSharp/" + Version;

            var timeout = request.Timeout != 0
                                ? request.Timeout
                                : Timeout;

            if (timeout != 0)
            {
                http.Timeout = timeout;
            }

            var readWriteTimeout = request.ReadWriteTimeout != 0
                                ? request.ReadWriteTimeout
                                : ReadWriteTimeout;

            if (readWriteTimeout != 0)
            {
                http.ReadWriteTimeout = readWriteTimeout;
            }

            http.FollowRedirects = FollowRedirects;

            if (ClientCertificates != null)
            {
                http.ClientCertificates = ClientCertificates;
            }

            http.MaxRedirects = MaxRedirects;
            http.CachePolicy  = CachePolicy;
            http.Pipelined    = Pipelined;

            if (request.Credentials != null)
            {
                http.Credentials = request.Credentials;
            }

            if (!string.IsNullOrEmpty(ConnectionGroupName))
            {
                http.ConnectionGroupName = ConnectionGroupName;
            }

            http.Headers = requestParameters
                           .Where(p => p.Type == ParameterType.HttpHeader)
                           .Select(p => new HttpHeader {
                Name = p.Name, Value = Convert.ToString(p.Value)
            })
                           .ToList();

            http.Cookies = requestParameters
                           .Where(p => p.Type == ParameterType.Cookie)
                           .Select(p => new HttpCookie {
                Name = p.Name, Value = Convert.ToString(p.Value)
            })
                           .ToList();

            http.Parameters = requestParameters
                              .Where(p => p.Type == ParameterType.GetOrPost && p.Value != null)
                              .Select(p => new HttpParameter {
                Name = p.Name, Value = Convert.ToString(p.Value)
            })
                              .ToList();

            http.Files = request.Files.Select(file => new HttpFile
            {
                Name          = file.Name,
                ContentType   = file.ContentType,
                Writer        = file.Writer,
                FileName      = file.FileName,
                ContentLength = file.ContentLength
            }).ToList();

            http.AddBody(requestParameters, Serializers, request.XmlSerializer, request.JsonSerializer);

            http.AllowedDecompressionMethods = request.AllowedDecompressionMethods;

            var proxy = Proxy ?? WebRequest.DefaultWebProxy;

            try
            {
                if (proxy == null)
                {
                    proxy = WebRequest.GetSystemWebProxy();
                }
            }
            catch (PlatformNotSupportedException)
            {
                // Ignore platform unsupported proxy detection
            }

            http.Proxy = proxy;

            http.RemoteCertificateValidationCallback = RemoteCertificateValidationCallback;

            return(http);
        }