コード例 #1
0
        private async Task <T> ReadHttpResponse <T>(HttpResponseMessage response, string requestUri, double timeoutInSec)
            where T : class
        {
            //  bool wait = queryTask.Wait(TimeSpan.FromSeconds(timeoutInSec));
            if (response.IsSuccessStatusCode)
            {
                T ret;

                if (typeof(Stream).IsAssignableFrom(typeof(T)))
                {
                    ret = await response.Content.ReadAsStreamAsync() as T;
                }
                else if (typeof(string).IsAssignableFrom(typeof(T)))
                {
                    ret = await response.Content.ReadAsStringAsync() as T;
                }
                else if (typeof(T) == typeof(byte[]))
                {
                    ret = await response.Content.ReadAsByteArrayAsync() as T;
                }
                else
                {
                    ret = await response.Content.ReadAsAsync <T>();
                }
                HttpMetadataHelper.SetObjectValuesFromHeaders(ret, response.Headers);
                HttpMetadataHelper.SetObjectValuesFromHeaders(ret, response.Content.Headers);
                return(ret);
            }

            throw new HttpRequestException($"{response.Content.ReadAsStringAsync().Result} - Code: {response.StatusCode} - Request: {requestUri}");
        }
コード例 #2
0
        public async Task <T> Head <T>(string request, double timeoutInSec = 30.0, CancellationToken token = default(CancellationToken))
            where T : class, new()
        {
            return(await HttpCall(async (client, uri) =>
            {
                HttpRequestMessage headRequest = new HttpRequestMessage(HttpMethod.Head, uri);
                HttpResponseMessage response = await client.SendAsync(headRequest, token);
//                bool wait = response.Wait(TimeSpan.FromSeconds(timeoutInSec));
                if (response.IsSuccessStatusCode)
                {
                    T obj = new T();
                    HttpMetadataHelper.SetObjectValuesFromHeaders(obj, response.Headers);
                    HttpMetadataHelper.SetObjectValuesFromHeaders(obj, response.Content.Headers);
                    return obj;
                }

                throw new HttpRequestException(response.ToString());
            }, HttpMethod.Head, request));
        }