コード例 #1
0
        // PUT
        public static async Task <DailyPrice> UpdateDailyPrice(DailyPrice price)
        {
            using (var client = new HttpClient())
            {
                // initialise the HttpClient
                client.BaseAddress = new Uri(URLBase);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                // HTTP PUT
                HttpResponseMessage response = await client.PutAsJsonAsync($"api/dailyprices/{price.Id}", price);

                //return await response.Content.ReadAsAsync<DailyPrice>();

                // check the reponse code
                if (response.IsSuccessStatusCode)
                {
                    // Deserialize the updated price data from the response body
                    return(await response.Content.ReadAsAsync <DailyPrice>());
                }
                else
                {
                    throw new ApplicationException(response.StatusCode.ToString());
                }
            }
        }
コード例 #2
0
        // POST
        public static async Task <DailyPrice> PostDailyPrice(DailyPrice price)
        {
            try
            {
                using (var client = new HttpClient())
                {
                    // initialise the HttpClient
                    client.BaseAddress = new Uri(URLBase);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    // HTTP GET
                    HttpResponseMessage response = await client.PostAsJsonAsync("api/dailyprices", price);

                    //return await response.Content.ReadAsAsync<DailyPrice>();

                    // check the reponse code
                    if (response.IsSuccessStatusCode)
                    {
                        return(await response.Content.ReadAsAsync <DailyPrice>());
                    }
                    else
                    {
                        throw new ApplicationException(response.StatusCode.ToString());
                    }
                }
            }
            catch (WebException ex) when(ex.Status == WebExceptionStatus.ProtocolError)
            {
                //code specifically for a WebException ProtocolError
                throw new ApplicationException("Error: WebException ProtocolError");
            }
            catch (WebException ex) when((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound)
            {
                //code specifically for a WebException NotFound
                throw new ApplicationException("Error: WebException NotFound");
            }
            catch (WebException ex) when((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.InternalServerError)
            {
                //code specifically for a WebException InternalServerError
                throw new ApplicationException("Error: WebException InternalServerError");
            }
        }