Пример #1
0
        void PrepareRequest(ref HttpRequestMessage request)
        {
            // post-condition: request != null && !string.IsNullOrEmpty(request.Method) && request.Uri != null && request.Uri.IsAbsoluteUri && !request.HasBeenSent;
            if (disposed && request != null)
            {
                request.Dispose();
            }
            ThrowIfDisposed();

            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            bool hasDefaultBaseAddress = this.address != null;

            if (request == null)
            {
                if (!hasDefaultBaseAddress)
                {
                    throw new ArgumentNullException("request", "request is null and BaseAddress is null");
                }

                request = new HttpRequestMessage();
            }

            if (request.HasBeenSent)
            {
                request.Dispose();
                throw new InvalidOperationException(request + " has already been sent");
            }

            if (request.Uri == null)
            {
                if (!hasDefaultBaseAddress)
                {
                    request.Dispose();
                    throw new ArgumentNullException("request", "request.Uri is null and DefaultRequest.BaseAddress is null");
                }
                request.Uri = this.BaseAddress;
            }
            else if (!request.Uri.IsAbsoluteUri)
            {
                if (!hasDefaultBaseAddress)
                {
                    request.Dispose();
                    throw new UriFormatException("request.Uri is relative (" + request.Uri + ") and DefaultRequest.BaseAddress is null");
                }
                request.Uri = new Uri(this.BaseAddress, request.Uri);
            }

            if (string.IsNullOrEmpty(request.Method))
            {
                request.Dispose();
                throw new ArgumentOutOfRangeException("request", "request.Method is null or empty");
            }

            if (defaultHeaders != null)
            {
                foreach (var h in defaultHeaders.Keys)
                {
                    if (request.Headers.ContainsKey(h))
                    {
                        continue;
                    }
                    foreach (var v in defaultHeaders.GetValues(h))
                    {
                        request.Headers.Add(h, v);
                    }
                }
            }

            request.HasBeenSent = true;
        }