示例#1
0
        public async Task <Employee> Get(int id)
        {
            // Set the Accept header.
            _httpClient.DefaultRequestHeaders.Clear();
            _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_dataFormat));
            string url = @"http://localhost:{_dataWarehousePortNumber}/api/employees/{id}";
            // Make a remote request.
            HttpResponseMessage response = await _httpClient.GetAsync(url);

            // Read response body and deserialize it.
            string body = await response.Content.ReadAsStringAsync();

            if (_dataFormat == "application/xml")
            {
                // Validate XML document.
                ValidateXmlSchema(body);
                // Deserialize XML document.
                Employee employee = await UtilityXml.DeserializeXmlAsync <Employee>(body);

                return(employee);
            }

            if (_dataFormat == "application/json")
            {
                Employee employee = await UtilityJson.DeserializeJsonAsync <Employee>(body);

                return(employee);
            }

            return(null);
        }
        public async Task <List <Employee> > Get(int offset, int limit)
        {
            // Set the Accept header.
            _httpClient.DefaultRequestHeaders.Clear();
            _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_dataFormat));
            string url = @"http://localhost:" + _dataWarehousePortNumber + "/api/employees/?offset= " + offset + "&limit= " + limit;
            // Make a remote request.
            HttpResponseMessage response = await _httpClient.GetAsync(url);

            // Read response body and deserialize it.
            string body = await response.Content.ReadAsStringAsync();

            if (_dataFormat == "application/xml")
            {
                // Validate XML document.
                ValidateXmlSchema(body);
                // Deserialize XML document.
                Employee[] employees = await UtilityXml.DeserializeXmlAsync <Employee[]>(body);

                return(employees.ToList());
            }

            if (_dataFormat == "application/json")
            {
                Employee[] employees = await UtilityJson.DeserializeJsonAsync <Employee[]>(body);

                return(employees.ToList());
            }

            // Return empty list.
            return(new List <Employee>());
        }