예제 #1
0
        private IWebRequest GetGetRequest(string url)
        {
            IWebRequest request = _webRequestProvider.CreateRequest(new Uri(url));

            request.Method = "GET";
            return(request);
        }
예제 #2
0
        public void it_should_create_a_web_request()
        {
            var expected = "http://temp.uri/";
            var result   = _webRequestProvider.CreateRequest(new Uri(expected), new Dictionary <string, string>()
            {
                { "Accept", "*/*" }, { "Location", expected }
            });

            result.Should().BeOfType <HttpWebRequest>().Which.Headers["Location"].Should().Be(expected);
        }
예제 #3
0
        /// <summary>Calls the ReST service using specified HTTP verb.</summary>
        /// <param name="verb">The HTTP verb.</param>
        /// <param name="url">Relative URL of the service to call.</param>
        /// <param name="accept">Enumeration of accepted media types.</param>
        /// <param name="contentType">Enumeration of possible content type media types.</param>
        /// <param name="responseType">Type of the response.</param>
        /// <param name="uriArguments">The URI template arguments.</param>
        /// <param name="bodyArguments">The body arguments.</param>
        /// <returns>Result of the call.</returns>
        protected object Call(Verb verb, string url, IEnumerable <string> accept, IEnumerable <string> contentType, Type responseType, IDictionary <string, object> uriArguments, params object[] bodyArguments)
        {
            var callUrl     = BuildUrl(url, uriArguments);
            var validAccept = (!accept.Any() ? _converterProvider.SupportedMediaTypes :
                               _converterProvider.SupportedMediaTypes.Join(accept, outer => outer, inner => inner, (outer, inner) => inner));
            var        accepted = (validAccept.Any() ? validAccept : new[] { "*/*" });
            WebRequest request  = _webRequestProvider.CreateRequest((Uri)callUrl, new Dictionary <string, string>()
            {
                { Header.Accept, String.Join(", ", accepted) }
            });

            if ((!String.IsNullOrEmpty(CredentialCache.DefaultNetworkCredentials.UserName)) && (!String.IsNullOrEmpty(CredentialCache.DefaultNetworkCredentials.Password)))
            {
                var credentials = new CredentialCache();
                credentials.Add(new Uri(callUrl.Authority), _authenticationScheme, new NetworkCredential(CredentialCache.DefaultNetworkCredentials.UserName, CredentialCache.DefaultNetworkCredentials.Password));
                request.Credentials     = credentials;
                request.PreAuthenticate = true;
            }

            request.Method = verb.ToString();
            if ((bodyArguments != null) && (bodyArguments.Length > 0))
            {
                FillRequestBody(verb, callUrl, request, contentType, accepted, bodyArguments);
            }

            var response = (HttpWebResponse)request.GetResponse();

            ParseContentRange(response, uriArguments);
            if (responseType == null)
            {
                return(null);
            }

            RequestInfo fakeRequest = new RequestInfo(verb, callUrl, response.GetResponseStream(), new BasicClaimBasedIdentity(), HeaderCollection.Parse(response.Headers.ToString()));
            var         result      = _resultBinder.BindResults(responseType, fakeRequest);

            return(result.FirstOrDefault(responseType.IsInstanceOfType));
        }