Address Insight C# Rest Code Snippet

protected void btn_Validate_Click(object sender, EventArgs e)
{
    string businessName, address1, address2, city, state, zip, testType, licenseKey;
    businessName = BusinessName.Text; address1 = Address1.Text; address2 = Address2.Text;
    city = City.Text; state = State.Text; zip = Zip.Text;
    testType = TestType.Text; licenseKey = inputLicenseKey.Text;
    try
    {
        if (String.IsNullOrWhiteSpace(licenseKey))
            licenseKey = "yourDevKey";
        AINResponse response = MakeRequest(businessName, address1, address2, city, state, zip, testType, licenseKey);
        ProcessResponse(response);
    }
    catch (Exception ex)
    {
        ErrorLabel.Text = ex.Message;
        ErrorLabel.Visible = true;
    }
}
public static AINResponse MakeRequest(string businessName, string address1, string address2, string city, string state, string zip, string testType, string licenseKey)
{
    string mainURL = WEB_SERVICE_PRIMARY_URL + "?BusinessName=" + businessName + "&Address1=" + address1 + "&Address2=" + address2 + "&City=" + city + "&State=" + state + "&Zip=" + zip + "&TestType="+ testType + "&LicenseKey=" + licenseKey;
    string backupURL = WEB_SERVICE_BACKUP_URL + "?BusinessName=" + businessName + "&Address1=" + address1 + "&Address2=" + address2 + "&City=" + city + "&State=" + state + "&Zip=" + zip + "&TestType=" + testType + "&LicenseKey=" + licenseKey;
    AINResponse result = null;
  
    try
    {
        result = HttpGet(mainURL);
        //NULL ERROR || FATAL ERROR RETURNED -- TRY BACKUP
        if (result == null || (result.error != null && result.error.TypeCode == "3"))
        {
            return HttpGet(backupURL);
        }
        else
        {
            return result;
        }
    }
    catch (Exception)
    {   //ERROR IN MAIN URL - USING BACKUP
        return HttpGet(backupURL);
    }
}
public static AINResponse HttpGet(string requestUrl)
{
    try
    {
        //NOTE: URL encoding occurs automatically when creating the web request
        HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
        request.Timeout = WEB_SERVICE_REQUEST_TIMEOUT;//timeout for get operation
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            if (response.StatusCode != HttpStatusCode.OK)
                throw new Exception(String.Format(
                "Server error (HTTP {0}: {1}).",
                response.StatusCode,
                response.StatusDescription));
            //parse response
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(AINResponse));
            object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
            AINResponse jsonResponse = objResponse as AINResponse;
            return jsonResponse;
        }
    }
    catch (Exception e)
    {
        throw e;
    }
}
private void ProcessResponse(AINResponse response)
{
    try
    {
        //processing result
        if (response.error == null)
        {
            ProcessResult(response);
        }
        //processing error
        else
        {
            ProcessError(response.error);
        }
    }
    catch (Exception e)
    {
        throw e;
    }
}
private void ProcessResult(AINResponse response)
{
    DataTable dtProvider = new DataTable();
    dtProvider.Columns.Add(new DataColumn("Output", typeof(string)));
    dtProvider.Columns.Add(new DataColumn("Values", typeof(string)));
    try
    {
        dtProvider.Rows.Add("Status", response.Status);
        dtProvider.Rows.Add("StatusScore", response.StatusScore);
        dtProvider.Rows.Add("AddressStatus", response.AddressStatus);
        dtProvider.Rows.Add("DPV", response.DPV);
        dtProvider.Rows.Add("DPVDesc", response.DPVDesc);
        ...
        dtProvider.Rows.Add("GeocodeStatus", response.GeocodeStatus);
        dtProvider.Rows.Add("LocationLatitude", response.LocationLatitude);
        dtProvider.Rows.Add("LocationLongitude", response.LocationLongitude);
        ...
        dtProvider.Rows.Add("CityType", response.CityType);
        dtProvider.Rows.Add("CityAliasName", response.CityAliasName);
        dtProvider.Rows.Add("AreaCode", response.AreaCode);
        dtProvider.Rows.Add("TimeZone", response.TimeZone);
        dtProvider.Rows.Add("DaylightSaving", response.DaylightSaving);
        ...
        dtProvider.Rows.Add("StateHouseholdIncome", response.StateHouseholdIncome);
        dtProvider.Rows.Add("ZipNotes", response.ZipNotes);
        dtProvider.Rows.Add("ZipNotesCodes", response.ZipNotesCodes);
        ResultGrid.Visible = true;
        ErrorGrid.Visible = false;
        ResultGrid.DataSource = new DataView(dtProvider);
        ResultGrid.DataBind();
    }
    catch (Exception e)
    {
        throw e;
    }
}
private void ProcessError(Error error)
{
    try
    {
        DataTable dtError = new DataTable();
          
        dtError.Columns.Add(new DataColumn("Output", typeof(string)));
        dtError.Columns.Add(new DataColumn("Values", typeof(string)));
        dtError.Rows.Add("Type", string.IsNullOrEmpty(error.Type) ? "" : error.Type);
        dtError.Rows.Add("TypeCode", string.IsNullOrEmpty(error.TypeCode) ? "" : error.TypeCode);
        dtError.Rows.Add("Desc", string.IsNullOrEmpty(error.Desc) ? "" : error.Desc);
        dtError.Rows.Add("DescCode", string.IsNullOrEmpty(error.DescCode) ? "" : error.DescCode);
  
        ErrorGrid.Visible = true;
        ResultGrid.Visible = false;
        ErrorGrid.DataSource = new DataView(dtError);
        ErrorGrid.DataBind();
    }
    catch (Exception e)
    {
        throw e;
    }
}

Address Insight Java Rest Code Snippet

String businessName = request.getParameter("businessName");
String address1 = request.getParameter("address1");
String address2 = request.getParameter("address2");
String city = request.getParameter("city");
String state = request.getParameter("state");
String zip = request.getParameter("zip");
String testType = request.getParameter("testType");
String licenseKey = request.getParameter("licensekey");
businessName = URLEncoder.encode(businessName,"UTF-8").replaceAll("+", "%20");
address1 = URLEncoder.encode(address1,"UTF-8").replaceAll("+", "%20");
address2 = URLEncoder.encode(address2,"UTF-8").replaceAll("+", "%20");
city = URLEncoder.encode(city,"UTF-8").replaceAll("+", "%20");
state = URLEncoder.encode(state,"UTF-8").replaceAll("+", "%20");
zip = URLEncoder.encode(zip,"UTF-8").replaceAll("+", "%20");
testType = URLEncoder.encode(testType,"UTF-8").replaceAll("+", "%20");
licenseKey = URLEncoder.encode(licenseKey,"UTF-8").replaceAll("+", "%20");
 
String mainURL = "https://trial.serviceobjects.com/AIN/api.svc/json/GetAddressInsight?BusinessName=" + businessName + "&Address1=" + address1 + "&Address2=" + address2 + "&City=" + city + "&State=" + state + "&Zip=" + zip + "&TestType=" + testType + "&LicenseKey=" + licenseKey;
String backupURL = "https://trial.serviceobjects.com/AIN/api.svc/json/GetAddressInsight?BusinessName=" + businessName + "&Address1=" + address1 + "&Address2=" + address2 + "&City=" + city + "&State=" + state + "&Zip=" + zip + "&TestType=" + testType + "&LicenseKey=" + licenseKey;
URL url = null;
HttpsURLConnection conn = null;
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
String responseString = "";
String prettyjson = "";
try{
url = new URL(mainURL);
conn = (HttpsURLConnection) url.openConnection();
conn.setConnectTimeout(10000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
while ((output = br.readLine()) != null) {
responseString += output;
}
 
JsonParser parser = new JsonParser();
JsonObject json = parser.parse(responseString).getAsJsonObject();
 
prettyjson = gson.toJson(json);
} catch (Exception ex) {
//call the backup client
url = new URL(backupURL);
conn = (HttpsURLConnection) url.openConnection();
conn.setConnectTimeout(10000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
while ((output = br.readLine()) != null) {
responseString += output;
}
 
JsonParser parser = new JsonParser();
JsonObject json = parser.parse(responseString).getAsJsonObject();
 
prettyjson = gson.toJson(json);
}