Search code examples
c#asp.netservicehttpwebrequest

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=List&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.


Solution

  • 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:

    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).