Address Geocode – US C# Code Snippet

GCRService.GCRSoapServiceClient client = new GCRService.GCRSoapServiceClient();
GCRService.GCRSoapServiceClient clientbackup = new GCRService.GCRSoapServiceClient();
GCRService.Location_V4 response = new GCRService.Location_V4();
try
{
    response = client.GetBestMatch_V4(Address.Text, City.Text, State.Text, PostalCode.Text, LicenseKey.Text);
    if (response == null || (response.Error != null && response.Error.Number == "4"))
    {
        throw new Exception();
    }
}
catch
{
    response = clientbackup.GetBestMatch_V4(Address.Text, City.Text, State.Text, PostalCode.Text, LicenseKey.Text);
}
if (response.Error == null)
{
    ProcessValidResponse(response);
}
else
{
    ProcessErrorResponse(response.Error);
}

Address Geocode – US Java Code Snippet

GCRSoapServiceLocator locator = new GCRSoapServiceLocator();
//use ssl
locator.setDOTSGeoCoderEndpointAddress("https://trial.serviceobjects.com/GCR/soap.svc/SOAP");
IGCRSoapService gcr = locator.getDOTSGeoCoder();
DOTSGeoCoderStub soap = (DOTSGeoCoderStub)gcr;
soap.setTimeout(5000);
  
try
{
    outputs = soap.getBestMatch_V4(address, city, state, postalcode, licenseKey);
    gcrerror = outputs.getError();
      
    if(gcrerror !=null && gcrerror.getNumber() == "4")
    {
        throw new Exception(); 
    }
}
catch(Exception e)
{   //FAILOVER- USE BACKUP NOW
    try{
        //CALL SOAP USING BACKUP URL (Change the endpoint)
        outputs = soap.getBestMatch_V4(address, city, state, postalcode, licenseKey);
        gcrerror = outputs.getError();
    }
    catch(Exception ex)
    {
      
        throw new Exception("Both Primary and Backup soap calls failed: " + ex.getMessage());
    }
} 
}

Address Geocode – US PHP Code Snippet

$URL = "https://trial.serviceobjects.com/GCR/soap.svc?singleWsdl";
//use backup url once given purchased license key
$backupURL = "https://trial.serviceobjects.com/GCR/soap.svc?singleWsdl";
   
$client = new SoapClient($URL);
//$soapClient = new SoapClient($URL, array( "trace" => 1 ));
$outputs = $client->GetBestMatch_V4($params)->GetBestMatch_V4Result;
if (empty($outputs))
{
    $backupClient = new SoapClient($backupURL);
    $outputs = $client->GetBestMatch_V4($params)->GetBestMatch_V4Result;
}

Address Geocode – US RoR Code Snippet

dotsGCRPrimary = "https://trial.serviceobjects.com/gcr/soap.svc?wsdl"
dotsGCRBackup = "https://trial.serviceobjects.com/gcr/soap.svc?wsdl"
  
begin
    #initializes the soap client. The convert request keys global is necessary to receive a response from the service.
    client = Savon.client(  wsdl: dotsGCRPrimary,
                            element_form_default: :qualified,
                            convert_request_keys_to: :camelcase
                         )
    #Calls the with given inptus and converts response to a hash.
    response = client.call(:get_best_match_v4, message: message).to_hash
    #Checks to see what results came back from the service
    processresults(response)           
      
#If an error occurs during the call, this will use the Backup url and attempt to retrieve data.
rescue Savon::Error => e
    begin
    backupclient = Savon.client(    wsdl: dotsGCRBackup,
                                    element_form_default: :qualified,
                                    convert_request_keys_to: :camelcase
                               )
    #Sets the response to the backclient call to the operation and converts response to a hash.
    response = backupclient.call(:get_best_match_v4, message: message).to_hash
    processresults(response)
    #If the backup url failed, this will display the error received from the server
    rescue Savon::Error =>error
        @status = error
        @displaydata = {"error" => "A Big Error Occured"}
    end
end

Address Geocode – US Python Code Snippet

mAddress =  Address.get()
if mAddress is None or  mAddress == "":
    mAddress = " "
mCity = City.get()
if mCity is None or mCity == "":
    mCity = " "
mState = State.get()
if mState is None or mState == "":
    mState = " "
mPostalCode = PostalCode.get()
if mPostalCode is None or mPostalCode == "":
    mPostalCode = " "
mLicenseKey = LicenseKey.get()
if mLicenseKey is None or mLicenseKey == "":
    mLicenseKey = " "
#Set the primary and backup URLs as needed
primaryURL = 'https://trial.serviceobjects.com/gcr/soap.svc?wsdl'
backupURL = 'https://trial.serviceobjects.com/gcr/soap.svc?wsdl'
#This block of code calls the web service and prints the resulting values to the screen
try:
    client = Client(primaryURL)
    result = client.service.GetBestMatch_V4(Address=mAddress, City=mCity, State=mState, PostalCode=mPostalCode, LicenseKey=mLicenseKey)
    #Loops through either the error result or proper result and displays values to the screen.
    if hasattr(result, 'Error') :
        for value in result.Error:
            Label(swin.window, text=str(value[0]) + " : " + str(value[1]) if value[1] else str(value[0])+": None").pack()
    #If a valid response was received from this service, this will print the result
    else:
        for value in result:
            if value[0] != 'InformationComponents':
                Label(swin.window, text=str(value[0]) + ":  " + str(value[1]) if str(value[1]) else str(value[0]) + " : None").pack()
        if "InformationComponents" in result:
            infoComponent = result.InformationComponents.InformationComponent
            for i in range(0, len(infoComponent)):
                Label(swin.window, text=str(infoComponent[i].Name) + ": " + str(infoComponent[i].Value)).pack()

Address Geocode – US ColdFusion Code Snippet

<cfscript>
        try
        {
            if (isDefined("form.Action") AND Action neq "")
            {
                wsresponse = CreateObject("webservice", "https://trial.serviceobjects.com/gcr/soap.svc?wsdl");                           
                outputs = wsresponse.getBestMatch_V4("#Address#", "#City#", "#State#", "#PostalCode#" ,"#LicenseKey#");
            }
        }
    catch(any Exception){
        try
            {
                if (isDefined("form.Action") AND Action neq "")
                {
                    wsresponse = CreateObject("webservice", "https://trial.serviceobjects.com/gcr/soap.svc?wsdl");                           
                    outputs = wsresponse.getBestMatch_V4("#Address#", "#City#", "#State#", "#PostalCode#" ,"#LicenseKey#");
                }
            }
            catch(any Exception)   
                {
                 writeoutput("An Error Has Occured. Please Reload and try again #Exception.message#");              
                }
        }
</cfscript>

Address Geocode – US Visual Basic Code Snippet

Try
    Dim ws As New GCRService.GCRSoapServiceClient
    Dim response As GCRService.Location_V4
    response = ws.GetBestMatch_V4(Address.Text, City.Text, State.Text, PostalCode.Text, LicenseKey.Text)
    If (response.Error Is Nothing) Then
        ProcessValidResponse(response)
    Else
        ProcessErrorResponse(response.Error)
    End If
  
Catch
    ''Set the Primary and Backup Service References as necessary
    Try
        Dim wsbackup As New GCRService.GCRSoapServiceClient
        Dim response As GCRService.Location_V4
        response = wsbackup.GetBestMatch_V4(Address.Text, City.Text, State.Text, PostalCode.Text, LicenseKey.Text)
        If (response.Error Is Nothing) Then
            ProcessValidResponse(response)
        Else
            ProcessErrorResponse(response.Error)
        End If
    Catch ex As Exception
        resultsLabel.Visible = True
        resultsLabel.Text = ex.Message
  
    End Try
  
End Try

Address Geocode – US Apex Code Snippet

wwwServiceobjectsCom.Location_V4 result;
try{
wwwServiceobjectsCom.DOTSGeoCoder client = new wwwServiceobjectsCom.DOTSGeoCoder();
result = client.GetBestMatch_V4([Address], [City], [State], [PostalCode], [LicenseKey]);
}
catch(Exception ex){
 //If the first request failed try the failover endpoint
wwwServiceobjectsCom.DOTSGeoCoder backupClient = new wwwServiceobjectsCom.DOTSGeoCoder();
//The backup environment will be provided to you upon purchasing a production license key
backupClient.endpoint_x = 'https://trial.serviceobjects.com/GCR/soap.svc/SOAP';
result = backupClient.GetBestMatch_V4([Address], [City], [State], [PostalCode], [LicenseKey]);
}

Address Geocode – US TSQL Code Snippet

SET @requestBody ='<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">'+
                   '<s:Body>'+
                   '<GetBestMatch_V4 xmlns="https://www.serviceobjects.com">'+
                   '<Address>' + @address + '</Address>'+
                   '<City>' + @City + '</City>'+
                   '<State>' + @State + '</State>'+
                   '<PostalCode>' + @postalcode + '</PostalCode>'+
                   '<LicenseKey>' + @key + '</LicenseKey>'+
                   '</GetBestMatch_V4>'+
                   '</s:Body>'+
                   '</s:Envelope>'
SET @requestLength = LEN(@requestBody)
    --If a production key is purchased, this will execute the failover
IF @isLiveKey = 1
BEGIN
    EXEC sp_OACreate 'MSXML2.ServerXMLHttp', @obj OUT
    EXEC sp_OAMethod @obj, 'Open', NULL, 'POST', 'https://trial.serviceobjects.com/GCR/soap.svc/SOAP', false
    EXEC sp_OAMethod @obj, 'setRequestHeader', NULL, 'HOST', 'sws.serviceobjects.com'
    EXEC sp_OAMethod @obj, 'setRequestHeader', NULL, 'Content-Type', 'text/xml; charset=UTF-8'
    EXEC sp_OAMethod @obj, 'setRequestHeader', NULL, 'SOAPAction', '"https://www.serviceobjects.com/IGCRSoapService/GetBestMatch_V4"'
    EXEC sp_OAMethod @obj, 'setRequestHeader', NULL, 'Content-Length', @requestLength
    EXEC sp_OAMethod @obj, 'send', NULL, @requestBody
    EXEC sp_OAGetProperty @obj, 'Status', @responseCode OUTPUT
    EXEC sp_OAGetProperty @obj, 'StatusText', @statusText OUTPUT
    EXEC sp_OAGetProperty @obj, 'responseText', @response OUTPUT
              
    --Checks the Response for a fatal error or if null.
    IF @response IS NULL
    BEGIN
        EXEC sp_OACreate 'MSXML2.ServerXMLHttp', @obj OUT
        EXEC sp_OAMethod @obj, 'Open', NULL, 'POST', 'https://trial.serviceobjects.com/GCR/soap.svc/SOAP', false
        EXEC sp_OAMethod @obj, 'setRequestHeader', NULL, 'HOST', 'swsbackup.serviceobjects.com'
        EXEC sp_OAMethod @obj, 'setRequestHeader', NULL, 'Content-Type', 'text/xml; charset=UTF-8'
        EXEC sp_OAMethod @obj, 'setRequestHeader', NULL, 'SOAPAction', '"https://www.serviceobjects.com/IGCRSoapService/GetBestMatch_V4"'
        EXEC sp_OAMethod @obj, 'setRequestHeader', NULL, 'Content-Length', @requestLength
        EXEC sp_OAMethod @obj, 'send', NULL, @requestBody
        EXEC sp_OAGetProperty @obj, 'Status', @responseCode OUTPUT
        EXEC sp_OAGetProperty @obj, 'StatusText', @statusText OUTPUT
        EXEC sp_OAGetProperty @obj, 'responseText', @response OUTPUT
    END
END

Address Geocode – US NodeJS Code Snippet

var args = {Address: 'Address',
            City: 'City',
            State: 'State',
            PostalCode: 'PostalCode',
            LicenseKey: 'Your-License-Key'};
soap.createClient(primaryUrl, function(err, client) {
      
    client.GetBestMatch_V4(args, function(err, result) {
        //This is where you will handle the service results. Your business logic will determine
        //how the validated information is used.
        //The exact output can be found in our documentation:
        //https://www.serviceobjects.com/docs/dots-address-geocode-us/
        if(err != null || result == null)
        {
            //There was an error that occurred that wasn't part of the normal service response
            return;
        }
        else{
            //Check for an error object
            if(result.GetBestMatch_V4Result.Error != null)
            {
                //An error object was returned by the service and you will want to use
                //the following failover logic.
                //If it was a Service Objects Fatal exception we recommend trying
                //a backup server.
                if(result.GetBestMatch_V4Result.Error.Number == "4")
                {
                    //The actual backup url will be provided when you purchase a license key
                    var backupUrl = 'https://trial.serviceobjects.com/gcr/soap.svc?wsdl';
                    soap.createClient(backupUrl, function(failoverErr, backupClient) {
      
                        backupClient.GetBestMatch_V4(args, function(failoverErr, failoverResult) {
                            //Handle the failoverErr or failoverResult objects.
                            return;
                        });
                    });
                }
                else{
                    //The Error object isn't of the type "Service Objects Fatal" so
                    //there is no need to use the failover logic. There was some Error of
                    //type Authorization, User Input, or Domain Specific.
                    response.writeHead(200, "OK", {'Content-Type': 'text/html'});
                    response.end(JSON.stringify(result));
                    return;
                }
            }
            else{
                //You have a non Error response.
                //All of the data will reside within result.GetBestMatchesResult
                //As an easy to see what the service returns I am returning a JSON
                //serialized version as the response.
                response.writeHead(200, "OK", {'Content-Type': 'text/html'});
                response.end(JSON.stringify(result));
                return;
            }
        }
        });
});