예제 #1
0
        //private HttpClientAdapter()
        //{ }


        public static System.Net.Http.HttpMethod AsNetMethod(Provider.HttpMethod method)
        {
            switch (method)
            {
            case Provider.HttpMethod.Delete:
                return(System.Net.Http.HttpMethod.Delete);

            case Provider.HttpMethod.Get:
                return(System.Net.Http.HttpMethod.Get);

            case Provider.HttpMethod.Post:
                return(System.Net.Http.HttpMethod.Post);

            case Provider.HttpMethod.Put:
                return(System.Net.Http.HttpMethod.Put);

            default:
                throw new ArgumentException("Cannot convert Provider.HttpMethod " + method.ToString() + " to System.Net.Http.HttpMethod");
            }
        }
예제 #2
0
        /// <summary>
        /// Creates a basic HttpWebRequest that can then be built off of depending on what other functionality is needed.
        /// </summary>
        /// <param name="uri">The uri to send the request to.</param>
        /// <param name="method">The Http Request Method.</param>
        /// <param name="requestMimeType">The MIME type of the data we are sending.</param>
        /// <param name="responseMimeType">The MIME we accept in response.</param>
        /// <returns>Returns an HttpWebRequest initialized with the given parameters.</returns>
        public static HttpClientAdapter CreateHttpClientAdapter(Uri uri, Provider.HttpMethod method, string responseMimeType, string requestMimeType)
        {
            CustomContract.Requires(uri != null);
            CustomContract.Requires(responseMimeType != null);
            CustomContract.Requires(method != Provider.HttpMethod.None);

            requestMimeType = requestMimeType ?? responseMimeType;

            var httpMessage = new HttpRequestMessage(AsNetMethod(method), uri);

            var httpClientAdapter = new HttpClientAdapter(new HttpClient(), httpMessage);

            if (method == Provider.HttpMethod.Post || method == Provider.HttpMethod.Put)
            {
                httpClientAdapter.requestMimeType = requestMimeType;
            }

            httpClientAdapter.HttpClient.DefaultRequestHeaders.Add("Accept", responseMimeType);

            return(httpClientAdapter);
        }