public AzureMapResults Get_Nav_CustomerCoordinates(Nav_Customer customer) { try { var address = string.Format("{0}, {1}, {2}, {3}, ({4})" , customer.Address , customer.PostCode , customer.City , customer.County , customer.CountryRegionCode); var url = string.Format(AzureMapEndpoint, AzureMapKey, address); using (var client = new WebClient()) { var uri = new Uri(url); var response = client.DownloadString(uri).ToString(); var result = JsonConvert.DeserializeObject <AzureMapResults>(response.ToString()); return(result); } } catch (Exception) { return(null); } }
public async Task <Nav_CustomerCoordinates> UpdateCustomer(string apiEntity, Nav_Customer customer, AzureMapResults azureMapResults, string filter) { try { AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false); var company = System.Net.WebUtility.UrlEncode(config.NavWebServiceCompany); var endPoint = string.Format(config.NavWebServiceMainUrl, company, apiEntity); var ntlmHandler = new HttpClientHandler { Credentials = new NetworkCredential(config.NavWebServiceUser, config.NavWebServicePassword, config.NavWebServiceDomain) }; using (var client = new HttpClient(ntlmHandler)) { var coords = new Nav_CustomerCoordinates(); coords.Prime365Lat = (decimal)azureMapResults.Results.ElementAt(0).Position.Lat; coords.Prime365Lng = (decimal)azureMapResults.Results.ElementAt(0).Position.Lon; coords.No = customer.No; var apiUpdateEndpoint = endPoint + filter; var request = new HttpRequestMessage(HttpMethod.Patch, new Uri(apiUpdateEndpoint)); var json = JsonConvert.SerializeObject(coords); request.Content = new StringContent(json, Encoding.UTF8, "application/json"); request.Headers.TryAddWithoutValidation("If-Match", customer.OdataEtag); var response = await client.SendAsync(request); if (response.StatusCode == HttpStatusCode.OK) { var responseJson = await response.Content.ReadAsStringAsync(); var customerCoord = JsonConvert.DeserializeObject <Nav_CustomerCoordinates>(responseJson); return(customerCoord); } return(null); } } catch (Exception) { return(null); } }