void PopulateWhitepagesData(List <string> phoneList, string apiKey) { int statusCode = -1; string description = string.Empty; string errorMessage = string.Empty; int rowNumToPopulate = 2; for (int i = 0; i < phoneList.Count; i++) { NameValueCollection nameValues = new NameValueCollection(); nameValues["phone"] = phoneList[i]; nameValues["api_key"] = apiKey; WhitePagesWebService webService = new WhitePagesWebService(); // Call method ExecuteWebRequest to execute backend API and return response stream. Stream responseStream = webService.ExecuteWebRequest(nameValues, ref statusCode, ref description, ref errorMessage); // Checking respnseStream null and status code. if (statusCode == 200 && responseStream != null) { // Reading response stream to StreamReader. StreamReader reader = new StreamReader(responseStream); // Convert stream reader to string JSON. string responseInJson = reader.ReadToEnd(); // Dispose response stream responseStream.Dispose(); // Calling ParsePhoneLookupResult to parse the response JSON in data Result class. Result resultData = ParsePhoneLookupResult(responseInJson); PopulateExcel(resultData, rowNumToPopulate++); } } }
/// <summary> /// Event occurs when the Button control is clicked. /// Here we will call phone lookup API, parse result and populate result on UI /// </summary> /// <param name="sender">Object sender</param> /// <param name="e">event data.</param> protected void FindPhoneResults() { try { //errorBox.Visible = false; //LiteralMessage.Text = string.Empty; string phoneQuery = Request.QueryString["phone"]; if (!string.IsNullOrEmpty(phoneQuery)) { int statusCode = -1; string description = string.Empty; string errorMessage = string.Empty; NameValueCollection nameValues = new NameValueCollection(); nameValues["phone"] = phoneQuery; nameValues["api_key"] = WhitePagesConstants.ApiKey; WhitePagesWebService webService = new WhitePagesWebService(); // Call method ExecuteWebRequest to execute backend API and return response stream. Stream responseStream = webService.ExecuteWebRequest(nameValues, ref statusCode, ref description, ref errorMessage); // Checking respnseStream null and status code. if (statusCode == 200 && responseStream != null) { // Reading response stream to StreamReader. StreamReader reader = new StreamReader(responseStream); // Convert stream reader to string JSON. string responseInJson = reader.ReadToEnd(); // Dispose response stream responseStream.Dispose(); // Calling ParsePhoneLookupResult to parse the response JSON in data Result class. Result resultData = ParsePhoneLookupResult(responseInJson); if (resultData != null) { PopulateRaffleTickets(resultData); // Calling function to populate data on UI. PopulateDataOnUI(resultData); } } else { //this.ResultDiv.Visible = false; //this.errorBox.Visible = true; //this.LiteralMessage.Text = errorMessage; } } else { //this.ResultDiv.Visible = false; //this.errorBox.Visible = true; //this.LiteralMessage.Text = WhitePagesConstants.PhoneBlankInputMessage; } } catch (Exception ex) { //this.ResultDiv.Visible = false; //this.errorBox.Visible = true; //this.LiteralMessage.Text = ex.Message; } }
/// <summary> /// Button click event for find /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void ButtonFindClick(object sender, EventArgs e) { try { errorBox.Visible = false; LiteralErrorMessage.Text = string.Empty; if (!string.IsNullOrEmpty(textBoxPhoneNumber.Text)) { int statusCode = -1; string description = string.Empty; string errorMessage = string.Empty; NameValueCollection nameValues = new NameValueCollection(); nameValues["phone"] = textBoxPhoneNumber.Text; nameValues["api_key"] = WhitePagesConstants.ApiKey; WhitePagesWebService webService = new WhitePagesWebService(); Stream responseStream = webService.ExecuteWebRequest(nameValues, ref statusCode, ref description, ref errorMessage); if (statusCode == 200 && responseStream != null) { StreamReader reader = new StreamReader(responseStream); string responseInJson = reader.ReadToEnd(); dynamic jsonObject = JsonConvert.DeserializeObject(responseInJson); // Dispose response stream responseStream.Dispose(); dynamic dictionaryObj = jsonObject.dictionary; string phoneKey = string.Empty; foreach (var data in jsonObject.results) { phoneKey = data.Value; break; } if (!string.IsNullOrEmpty(phoneKey)) { dynamic phoneKeyObject = dictionaryObj[phoneKey]; string lineType = (string)phoneKeyObject["line_type"]; string phoneNumber = (string)phoneKeyObject["phone_number"]; string countryCallingCode = (string)phoneKeyObject["country_calling_code"]; string carrier = (string)phoneKeyObject["carrier"]; bool doNotCall = (bool)phoneKeyObject["do_not_call"]; dynamic spamScoreObj = phoneKeyObject.reputation; string spamScore = (string)spamScoreObj["spam_score"]; phoneNumber = countryCallingCode + phoneNumber; string formatedPhoneNumber = String.Format("{0:#-###-###-####}", Convert.ToInt64(phoneNumber)); formatedPhoneNumber = "+" + formatedPhoneNumber; LitralPhoneNumber.Text = formatedPhoneNumber; LiteralPhoneCarrier.Text = carrier; LiteralPhoneType.Text = lineType; if (doNotCall) { LiteralDndStatus.Text = "Registered"; } else { LiteralDndStatus.Text = "Not Registered"; } ResultDiv.Visible = true; if (!string.IsNullOrEmpty(spamScore)) { LiteralSpanScore.Text = spamScore + "%"; } else { LiteralSpanScore.Text = "0%"; } // Extracting location key from Phone details under best_location object. dynamic bestLocationFromPhoneObj = phoneKeyObject.best_location; dynamic bestLocationIdFromPhoneObj = bestLocationFromPhoneObj.id; string locationKeyFromPhone = (string)bestLocationIdFromPhoneObj["key"]; // Starting to extarct the person information dynamic phoneKeyObjectBelongsToObj = phoneKeyObject.belongs_to; dynamic belongsToObj = null; List <string> personKeyListFromBelongsTo = new List <string>(); List <string> locationKeyList = new List <string>(); foreach (var data in phoneKeyObjectBelongsToObj) { belongsToObj = data.id; string personKeyFromBelongsTo = (string)belongsToObj["key"]; personKeyListFromBelongsTo.Add(personKeyFromBelongsTo); } if (belongsToObj != null) { dynamic personKeyObject = null; string peopleDetails = string.Empty; foreach (string personKey in personKeyListFromBelongsTo) { string peopleData = WhitePagesConstants.PeopleDataTemplates; personKeyObject = dictionaryObj[personKey]; dynamic phoneKeyIdObj = personKeyObject.id; string personType = phoneKeyIdObj["type"]; dynamic phoneKeyNamesObj = personKeyObject.names; string fullName = string.Empty; if (phoneKeyNamesObj != null) { string firstName = string.Empty; string lastName = string.Empty; string middleName = string.Empty; foreach (var name in phoneKeyNamesObj) { firstName = (string)name["first_name"]; middleName = (string)name["middle_name"]; lastName = (string)name["last_name"]; } fullName = firstName + " " + lastName; } else { fullName = personKeyObject.name; } peopleData = peopleData.Replace(":PEOPLE_NAME", fullName); peopleData = peopleData.Replace(":TYPE", personType); peopleDetails += peopleData; // Collecting Locations Key string locationKey = string.Empty; if (personKeyObject["best_location"] != null) { dynamic personBestLocationObj = personKeyObject.best_location; dynamic bestLocationIdObj = personBestLocationObj.id; locationKey = (string)bestLocationIdObj["key"]; } else { if (personKeyObject["locations"] != null) { dynamic locationsPerPersonObj = personKeyObject.locations; foreach (var location in locationsPerPersonObj) { dynamic locationIdObj = location.id; locationKey = (string)locationIdObj["key"]; break; } } } if (string.IsNullOrEmpty(locationKey)) { locationKey = locationKeyFromPhone; } locationKeyList.Add(locationKey); } LiteralPeopleDetails.Text = peopleDetails; if (personKeyObject != null) { string locationDetails = string.Empty; foreach (string locationKey in locationKeyList) { string locationDataTemplate = WhitePagesConstants.LocationDataTemplates; dynamic locationKeyObject = dictionaryObj[locationKey]; string standardAddressLine1 = (string)locationKeyObject["standard_address_line1"]; string standard_address_line2 = (string)locationKeyObject["standard_address_line2"]; string standard_address_location = (string)locationKeyObject["standard_address_location"]; bool isReceivingMail = false; if (locationKeyObject["is_receiving_mail"] != null) { isReceivingMail = (bool)locationKeyObject["is_receiving_mail"]; } string usage = (string)locationKeyObject["usage"]; string deliveryPoint = (string)locationKeyObject["delivery_point"]; string fullAddress = string.Empty; fullAddress += string.IsNullOrEmpty(standardAddressLine1) ? string.Empty : standardAddressLine1 + "<br />"; fullAddress += string.IsNullOrEmpty(standard_address_line2) ? string.Empty : standard_address_line2 + "<br />"; fullAddress += string.IsNullOrEmpty(standard_address_location) ? string.Empty : standard_address_location; string receivingMail = isReceivingMail ? "Yes" : "No"; locationDataTemplate = locationDataTemplate.Replace(":ADDRESS", fullAddress); locationDataTemplate = locationDataTemplate.Replace(":RECEIVING_MAIL", receivingMail); locationDataTemplate = locationDataTemplate.Replace(":USAGE", usage); locationDataTemplate = locationDataTemplate.Replace(":DELIVERY_POINT", deliveryPoint); locationDetails += locationDataTemplate; } LiteralLocationDetails.Text = locationDetails; } } } } else { ResultDiv.Visible = false; errorBox.Visible = true; LiteralErrorMessage.Text = errorMessage; } } else { ResultDiv.Visible = false; errorBox.Visible = true; LiteralErrorMessage.Text = "please enter phone number"; } } catch (Exception ex) { ResultDiv.Visible = false; errorBox.Visible = true; LiteralErrorMessage.Text = ex.Message; } }
/// <summary> /// FindButtonClick Event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void ButtonFind_Click(object sender, EventArgs e) { try { errorDiv.Visible = false; LitralErrorMessage.Text = string.Empty; if ((string.IsNullOrEmpty(person_first_name.Text))) { errorDiv.Visible = true; LitralErrorMessage.Text = "please enter first name."; return; } if (string.IsNullOrEmpty(person_last_name.Text)) { errorDiv.Visible = true; LitralErrorMessage.Text = "last_name value must be at least 1 characters"; return; } if (string.IsNullOrEmpty(person_where.Text)) { errorDiv.Visible = true; LitralErrorMessage.Text = "unparsed_location value must be at least 1 characters"; return; } int statusCode = -1; string description = string.Empty; string errorMessage = string.Empty; NameValueCollection nameValues = new NameValueCollection(); nameValues["first_name"] = person_first_name.Text; nameValues["last_name"] = person_last_name.Text; nameValues["address"] = person_where.Text; nameValues["api_key"] = WhitePagesConstants.ApiKey; WhitePagesWebService webService = new WhitePagesWebService(); Stream responseStream = webService.ExecuteWebRequest(nameValues, ref statusCode, ref description, ref errorMessage); if (statusCode == 200 && responseStream != null) { StreamReader reader = new StreamReader(responseStream); string responseInJson = reader.ReadToEnd(); // Dispose the response stream. responseStream.Dispose(); dynamic jsonObject = JsonConvert.DeserializeObject(responseInJson); dynamic dictionaryObj = jsonObject.dictionary; List <string> personKeyList = new List <string>(); foreach (var data in jsonObject.results) { personKeyList.Add(data.Value); } string personData = string.Empty; if (personKeyList.Count > 0) { foreach (string personKey in personKeyList) { string personDataTemplates = WhitePagesConstants.PersonDataTemplates; string ageSection = WhitePagesConstants.ageSection; dynamic personKeyObject = dictionaryObj[personKey]; dynamic ageRangeObject = personKeyObject["age_range"]; string personName = (string)personKeyObject["best_name"]; string ageRange = string.Empty; if (ageRangeObject != null) { ageRange = (string)ageRangeObject["start"] + "+"; } if (!string.IsNullOrEmpty(ageRange)) { ageSection = ageSection.Replace(":AGE", ageRange); } else { ageSection = string.Empty; } dynamic bestLocationObject = personKeyObject["best_location"]; dynamic bestLocationIdObject = bestLocationObject["id"]; string bestLocationKey = (string)bestLocationIdObject["key"]; string contentType = string.Empty; dynamic locationsObject = personKeyObject["locations"]; foreach (var location in locationsObject) { dynamic locationIdObject = location["id"]; string locationKey = (string)locationIdObject["key"]; if (locationKey.ToUpper() == bestLocationKey.ToUpper()) { contentType = (string)location["contact_type"]; } } dynamic locationKeyObject = dictionaryObj[bestLocationKey]; string addressLine1 = (string)locationKeyObject["standard_address_line1"]; string addressLine2 = (string)locationKeyObject["standard_address_line2"]; string addressLocation = (string)locationKeyObject["standard_address_location"]; string usage = (string)locationKeyObject["usage"]; bool isReceivingMail = false; if (locationKeyObject["is_receiving_mail"] != null) { isReceivingMail = (bool)locationKeyObject["is_receiving_mail"]; } string deliveryPoint = (string)locationKeyObject["delivery_point"]; string fullAddress = string.Empty; fullAddress += (string.IsNullOrEmpty(addressLine1) ? string.Empty : addressLine1 + "<br />"); fullAddress += (string.IsNullOrEmpty(addressLine2) ? string.Empty : addressLine2 + "<br />"); fullAddress += (string.IsNullOrEmpty(addressLocation) ? string.Empty : addressLocation + "<br />"); string receivingMail = isReceivingMail ? "Yes" : "No"; personDataTemplates = personDataTemplates.Replace(":FULL_NAME", personName); personDataTemplates = personDataTemplates.Replace(":AGE", ageSection); personDataTemplates = personDataTemplates.Replace(":FULL_ADDRESS", fullAddress); personDataTemplates = personDataTemplates.Replace(":RECEIVING_MAIL", receivingMail); personDataTemplates = personDataTemplates.Replace(":USAGE", usage); personDataTemplates = personDataTemplates.Replace(":DELIVERY_POINT", deliveryPoint); personDataTemplates = personDataTemplates.Replace(":TYPE", contentType); personData += personDataTemplates; } personResult.Visible = true; LiteralPersonResult.Text = personData; } } else { personResult.Visible = false; errorDiv.Visible = true; LitralErrorMessage.Text = errorMessage; } } catch (Exception ex) { errorDiv.Visible = true; LitralErrorMessage.Text = ex.Message; } }
/// <summary> /// Button click event for find /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void ButtonFindClick(object sender, EventArgs e) { try { errorBox.Visible = false; LiteralErrorMessage.Text = string.Empty; if ((string.IsNullOrEmpty(textBoxStreetLine1.Text) && string.IsNullOrEmpty(textBoxCity.Text)) || string.IsNullOrEmpty(textBoxStreetLine1.Text)) { errorBox.Visible = true; LiteralErrorMessage.Text = "please enter your address details."; return; } if (string.IsNullOrEmpty(textBoxCity.Text)) { errorBox.Visible = true; LiteralErrorMessage.Text = "city value must be at least 1 characters"; return; } int statusCode = -1; string description = string.Empty; string errorMessage = string.Empty; NameValueCollection nameValues = new NameValueCollection(); nameValues["street_line_1"] = textBoxStreetLine1.Text; nameValues["city"] = textBoxCity.Text; nameValues["api_key"] = WhitePagesConstants.ApiKey; WhitePagesWebService webService = new WhitePagesWebService(); Stream responseStream = webService.ExecuteWebRequest(nameValues, ref statusCode, ref description, ref errorMessage); if (statusCode == 200 && responseStream != null) { StreamReader reader = new StreamReader(responseStream); string responseInJson = reader.ReadToEnd(); // Dispose response stream responseStream.Dispose(); dynamic jsonObject = JsonConvert.DeserializeObject(responseInJson); dynamic dictionaryObj = jsonObject.dictionary; string locationKey = string.Empty; foreach (var data in jsonObject.results) { locationKey = data.Value; break; } if (!string.IsNullOrEmpty(locationKey)) { dynamic locationKeyObject = dictionaryObj[locationKey]; string addressLine1 = (string)locationKeyObject["standard_address_line1"]; string addressLine2 = (string)locationKeyObject["standard_address_line2"]; string addressLocation = (string)locationKeyObject["standard_address_location"]; string usage = (string)locationKeyObject["usage"]; bool isReceivingMail = (bool)locationKeyObject["is_receiving_mail"]; string deliveryPoint = (string)locationKeyObject["delivery_point"]; string fullAddress = string.Empty; fullAddress += (string.IsNullOrEmpty(addressLine1) ? string.Empty : addressLine1 + "<br />"); fullAddress += (string.IsNullOrEmpty(addressLine2) ? string.Empty : addressLine2 + "<br />"); fullAddress += (string.IsNullOrEmpty(addressLocation) ? string.Empty : addressLocation + "<br />"); LitralAddress.Text = fullAddress; LiteralReceivingMail.Text = isReceivingMail ? "Yes" : "No"; LiteralUsage.Text = usage; LiteralDeliveryPoint.Text = deliveryPoint; ResultDiv.Visible = true; dynamic locationLegalEntitiesObj = locationKeyObject.legal_entities_at; if (locationLegalEntitiesObj != null) { List <string> personKeyList = new List <string>(); foreach (var locationLegalData in locationLegalEntitiesObj) { dynamic locationLegalDataAtId = locationLegalData.id; string personKey = locationLegalDataAtId["key"]; personKeyList.Add(personKey); } LiteralPersonCount.Text = personKeyList.Count.ToString(); LiteralPersonDetails.Text = string.Empty; if (personKeyList.Count > 0) { string personDetails = string.Empty; foreach (string personKey in personKeyList) { string personDataTemplates = WhitePagesConstants.PersonDataTemplates; dynamic personKeyObject = dictionaryObj[personKey]; dynamic personIdObject = personKeyObject.id; string type = string.Empty; if (personIdObject != null) { type = (string)personIdObject["type"]; } string fullName = (string)personKeyObject["best_name"]; personDataTemplates = personDataTemplates.Replace(":PERSON_TYPE", type); personDataTemplates = personDataTemplates.Replace(":PERSON_NAME", fullName); personDetails += personDataTemplates; } LiteralPersonDetails.Text = personDetails; } } } } else { ResultDiv.Visible = false; errorBox.Visible = true; LiteralErrorMessage.Text = errorMessage; } } catch (Exception ex) { errorBox.Visible = true; LiteralErrorMessage.Text = ex.Message; } }