示例#1
0
        /// <summary>
        /// Send http put requset
        /// </summary>
        /// <typeparam name="ReturnType">
        /// The return type which data will be casted to
        /// </typeparam>
        /// <param name="requestedUrl">
        /// The api endpoint
        /// </param>
        /// <param name="setting">
        /// </param>
        /// <returns>
        /// Retrun the response body casted to to the type passed
        /// </returns>
        public ReturnType ExecutePutRequest <ReturnType>(string requestedUrl, ExecuteApiSetting setting)
        {
            HttpResponseMessage response = new HttpResponseMessage();
            HttpClientHandler   handler  = new HttpClientHandler
            {
                Credentials = setting.NetworkCredential,
                ServerCertificateCustomValidationCallback = setting.ServerCertificateCustomValidationCallback
            };

            using (HttpClient httpClient = new HttpClient(handler))
            {
                HttpRequestMessage request = new HttpRequestMessage
                {
                    Method     = HttpMethod.Put,
                    RequestUri = new Uri(requestedUrl),
                };

                response = httpClient.SendAsync(request).Result;
            }

            return(response.Content.ReadAsAsync <ReturnType>().Result);
        }
示例#2
0
        /// <summary>
        /// Send http get requset with query string paramerters
        /// </summary>
        /// <typeparam name="ReturnType">
        /// The return type which data will be casted to
        /// </typeparam>
        /// <param name="data">
        /// The paramter which will be passed to request
        /// </param>
        /// <param name="setting">
        /// </param>
        /// <param name="requestedUrl">
        /// The api endpoint
        /// </param>
        /// <returns>
        /// Retrun the response body casted to to the type passed
        /// </returns>

        public ReturnType ExecuteBodyGetRequest <ReturnType>(object data, string requestedUrl, ExecuteApiSetting setting)
        {
            HttpResponseMessage response = new HttpResponseMessage();
            HttpClientHandler   handler  = new HttpClientHandler
            {
                Credentials = setting.NetworkCredential,
                ServerCertificateCustomValidationCallback = setting.ServerCertificateCustomValidationCallback
            };

            using (HttpClient httpClient = new HttpClient(handler))
            {
                HttpRequestMessage request = new HttpRequestMessage
                {
                    Method     = HttpMethod.Get,
                    RequestUri = new Uri(requestedUrl),
                    Content    = new StringContent(data != null ? JsonConvert.SerializeObject(data) : "", Encoding.UTF8, "application/json"),
                };
                response = httpClient.SendAsync(request).Result;
            }
            return(response.Content.ReadAsAsync <ReturnType>().Result);
        }