I am trying to use the Despatch Bay shipping API within an ASP.NET application, but all their sample code is in PHP.
I've used web services before, but am having trouble passing the credentials to this.
The PHP sample code creates a new instance of the API as follows:
$soapOptions = array ('login' => APIUSER, 'password' => APIKEY);
$DespatchBay = new SoapClient("http://api.despatchbaypro.com/api/soap/v10/addressing?wsdl",$soapOptions);
Using Visual Web developer Express, I have added a service reference to my Project, and can now create an instance like this:
Dim dbService As New DespatchBayAddresses.AddressingServicePortTypeClient
And attempt to write out an address from a post code as follows:
Response.Write(dbService.GetDomesticAddressKeysByPostcode("INSERT POSTCODE").ToString())
But, quite naturally, I get an error:
The remote server returned an error: (401) Unauthorized.
I know why, because I haven't passed in my API_KEY
or API_USER
credentials.
But, I'm being dumb I am sure, but can't figure out how.
I tried
dbService.ClientCredentials.UserName.UserName = MY_USER
dbService.ClientCredentials.UserName.Password = MY_KEY
But same error. I need to be able to pass these values from a variable retrieved from my database, not a static value in web.config, so could someone please point me in the direction of getting these details to the API?
Thanks
Right... well, it turns out that the problem was that I was using the wrong type of web service.
Eventually, found this:
http://markfreedman.com/index.php/2010/10/13/web-reference-vs-service-reference/
Which pointed me in the right direction, added the Reference using the steps mentioned there and now have this (working) code:
Function getAddressesByPostcode(User As String, Api_Key As String, Postcode As String) As Dictionary(Of String, String)
Dim d As New com.despatchbaypro.api.AddressingService
d.Credentials = New NetworkCredential(User, Api_Key)
Dim Addresses = d.GetDomesticAddressKeysByPostcode(Postcode)
Dim dictionary As New Dictionary(Of String, String)
For Each address In Addresses
'Dim add As com.despatchbaypro.api.AddressDetailType = d.GetDomesticAddressByKey(address.Key)
dictionary.Add(address.Key, address.Address)
Next
Return dictionary
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.