Очень поздний ответ на этот вопрос, но я использую RestSharp для реализации этого точного сервиса.
Веб-служба, обнаруженная Робертом У, не отвечает моим потребностям, поскольку для получения полных адресных данных требуется 2 вызова: один для получения списка совпадений и один для получения выбранного адреса.
Служба XML предоставляет формат, который включает полные адресные данные для всех результатов сопоставления.
Вот мое решение.
Вам нужен пользовательский IAuthenticator
:
public class AfdAuthenticator : IAuthenticator
{
private readonly string serial;
private readonly string password;
private readonly string userId;
public AfdAuthenticator(string serial, string password, string userId)
{
this.serial = serial;
this.password = password;
this.userId = userId;
}
public void Authenticate(IRestClient client, IRestRequest request)
{
// AFD requires the authentication details to be included as query string parameters
request.AddQueryParameter("Serial", this.serial);
request.AddQueryParameter("Password", this.password);
request.AddQueryParameter("UserID", this.userId);
}
}
Вам понадобятся классы для ответа:
[XmlRoot(ElementName = "AFDPostcodeEverywhere")]
public class AfdPostcodeEverywhere
{
[XmlElement(ElementName = "Result")]
public int Result { get; set; }
[XmlElement(ElementName = "ErrorText")]
public string ErrorText { get; set; }
[XmlElement(ElementName = "Items")]
public List<Item> Items { get; set; }
}
[XmlRoot(ElementName = "Item")]
public class Item
{
[XmlElement(ElementName = "AbbreviatedPostalCounty")]
public string AbbreviatedPostalCounty { get; set; }
[XmlElement(ElementName = "OptionalCounty")]
public string OptionalCounty { get; set; }
[XmlElement(ElementName = "AbbreviatedOptionalCounty")]
public string AbbreviatedOptionalCounty { get; set; }
[XmlElement(ElementName = "PostalCounty")]
public string PostalCounty { get; set; }
[XmlElement(ElementName = "TraditionalCounty")]
public string TraditionalCounty { get; set; }
[XmlElement(ElementName = "AdministrativeCounty")]
public string AdministrativeCounty { get; set; }
[XmlElement(ElementName = "Postcode")]
public string Postcode { get; set; }
[XmlElement(ElementName = "DPS")]
public string Dps { get; set; }
[XmlElement(ElementName = "PostcodeFrom")]
public string PostcodeFrom { get; set; }
[XmlElement(ElementName = "PostcodeType")]
public string PostcodeType { get; set; }
[XmlElement(ElementName = "Phone")]
public string Phone { get; set; }
[XmlElement(ElementName = "Key")]
public string Key { get; set; }
[XmlElement(ElementName = "List")]
public string List { get; set; }
[XmlElement(ElementName = "Locality")]
public string Locality { get; set; }
[XmlElement(ElementName = "Property")]
public string Property { get; set; }
[XmlElement(ElementName = "Street")]
public string Street { get; set; }
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "Organisation")]
public string Organisation { get; set; }
[XmlElement(ElementName = "Town")]
public string Town { get; set; }
[XmlAttribute(AttributeName = "value")]
public int Value { get; set; }
}
Я сгенерировал их, вызвав службу, используя PostMan , а затем Xml2CSharp для генерации классов из XML
Тогда вы можете использовать этот код:
// Create a RestClient passing in a custom authenticator and base url
var client = new RestClient
{
Authenticator = new AfdAuthenticator("YOUR SERIAL", "YOUR PASSWORD", "YOUR USERID"),
BaseUrl = new UriBuilder("http", "pce.afd.co.uk").Uri
};
// Create a RestRequest using the AFD service endpoint and setting the Method to GET
var request = new RestRequest("afddata.pce", Method.GET);
// Add the required AFD query string parameters
request.AddQueryParameter("Data", "Address");
request.AddQueryParameter("Task", "FastFind");
request.AddQueryParameter("Fields", "Simple");
request.AddQueryParameter("Lookup", "BD1 3RA");
// Execute the request expecting an AfdPostcodeEverywhere returned
var response = client.Execute<AfdPostcodeEverywhere>(request);
// Check that RestSharp got a response
if (response.StatusCode != HttpStatusCode.OK)
{
throw new Exception(response.StatusDescription);
}
// Check that RestSharp was able to process the response
if (response.ResponseStatus != ResponseStatus.Completed)
{
throw new Exception(response.ErrorMessage, response.ErrorException);
}
var afdPostcodeEverywhere = response.Data;
// Check that AFD returned data
if (afdPostcodeEverywhere.Result < 0)
{
throw new Exception(afdPostcodeEverywhere.ErrorText);
}
// Process the results
var addresses = afdPostcodeEverywhere.Items;
Полную информацию о параметрах «Данные», «Задача», «Поля» и «Поиск», а также все коды результатов можно найти в документации AFD API, которая включена в SDK .