Query Post code Lookup Web service Via Code
I have a url which normally points to a postcode lookup webservice:
"http://localhost/afddata.pce?Serial=xxxxxx&Password=<PASSWORD>&UserID=<UNAME>&Data=Address&Task=PropertyLookup&Fields=Lis开发者_运维百科t&MaxQuantity=200&Lookup=BD1+3RA"
I need to make a call to this url, probably by using HttWebRequest and get the output which is an xml string (see example):
<?xml version="1.0"?>
<AFDPostcodeEverywhere>
<Result>1</Result><ErrorText></ErrorText><Item value="1"><Postcode>BD1 3RA</Postcode>
<PostcodeFrom></PostcodeFrom>
<Key>BD1 3RA1001</Key>
<List>BD1 3RA City of Bradford Metropolitan District Council, Fountain Hall, Fountain Street, BRADFORD</List>
<CountryISO>GBR</CountryISO>
</Item>
</AFDPostcodeEverywhere>
My problem is that when I type the URL in my browser, I get the XML above in my browser but I am not able to get this XML string via code. From what I have read we need to make a soap request but I don't know how to do that.
You can get the XML response from the HttpWebResponse object (under the System.Net namespace if I can remember).
To get a HttpWebResponse you first have to build up a HttpWebRequest object.
See:
- HttpWebResponse
- HttpWebRequest
And you can use the following code to convert the response into an XMLDocument that you can traverse:
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://pce.afd.co.uk/afddata.pce?...");
using (HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse())
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(HttpWResp.GetResponseStream());
}
EDIT (1):
The company in question appear to have an ASMX webservice that you will be able to consume in your .NET application to get to the neccessary data.
You'll need the password and serial number (which you can get from the URL).
Personally, I prefer to use XDocument.Load method and then use LINQ-to-XML to read through the results. It's cleaner than other parsing mechanisms in the .NET framework.
var xmlDoc = XDocument.Load("http://localhost/afddata.pce?Serial=xxxxxx&Password=<PASSWORD>&UserID=<UNAME>&Data=Address&Task=PropertyLookup&Fields=List&MaxQuantity=200&Lookup=BD1+3RA");
You could then use LINQ to parse the results or, alternatively, call ToString() to get the XML as a string.
Very late answer to this but I am using RestSharp to implement this exact service.
The web service discovered by Robert W does not meet my needs as it requires 2 calls to get the full address data, one to get the list of matches and one to get the selected address.
The XML service provides a format which includes the full address data for all of the matching results.
Here's my solution.
You need a custom 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);
}
}
You will need classes for the response:
[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; }
}
I generated these by calling the service using PostMan then using Xml2CSharp to generate the classes from the XML
Then you can use this code:
// 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;
Full details of the Data, Task, Fields and Lookup options as well as all of the Result codes can be found in the AFD API documentation which is included in the SDK.
精彩评论