Connecting to API Using ASP.NET

ASP.NET-Logo Included here you will find simple sample code for parsing the Database Interface API in XML. The first example returns primary data for a given jurisdiction - the result containing the United States Post Office. Some ZIP codes include multiple cities and sometimes multiple tax jurisdictions, so the second example shows how to go down the hierarchy and parse the XML.

Basic: sample header for NewASP.NET

		<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="mssql-basic.vb" Inherits="VBBasic" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

		
Basic: mssql-basic.vb

		
Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Xml


Partial Class VBBasic
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strZip As String = "90210"
        'sample zip code must be between 90001 and 90999
        Dim strUser As String = "sample"
        Dim strPassword As String = "password"
        Dim url As String
        url = "https://api.zip2tax.com/TaxRate-USA.xml?username="&strUser&"&password="&strPassword&"&zip="&strZip

        Dim request As HttpWebRequest
        Dim http_response As HttpWebResponse = Nothing
   
        'Create the web request  
        request = DirectCast(WebRequest.Create(url), HttpWebRequest)

        ' Get response  
        http_response = DirectCast(request.GetResponse(), HttpWebResponse)

        ' Get the response stream into a reader  
        Dim reader As StreamReader = New StreamReader(http_response.GetResponseStream(), Encoding.UTF8)

        Dim str As String = reader.ReadLine()

        Dim doc As XDocument = XDocument.Parse(str)
        Dim generalElement As XElement = doc.Element("z2tLookup").Element("errorInfo")

        Dim errorInfo As String = generalElement.Element("errorMessage").Value

        If errorInfo = "Success" Then

            Dim generalInnerElement As XElement =
                    doc.Element("z2tLookup").Element("addressInfo").Element("addresses").Element("address")

            Dim generalInnerTaxElement As XElement =
                generalInnerElement.Element("salesTax").Element("rateInfo")

            Dim generalInnerNotesElement As XElement =
                    generalInnerElement.Element("notes")

            Dim childElement As XElement
            Dim shipping As Integer = 0

            For Each childElement In generalInnerNotesElement.Descendants("noteDetail").Elements("note")

                If childElement.Value.ToString().Equals("Shipping charges are not taxable") Then

                    shipping = 1
                End If
            Next

            Response.Write("Zip Code: " + generalInnerElement.Element("zipCode").Value + "
") Response.Write("Sales Tax Rate: " + generalInnerTaxElement.Element("taxRate").Value + "
") Response.Write("Post Office City: " + generalInnerElement.Element("place").Value + "
") Response.Write("County: " + generalInnerElement.Element("county").Value + "
") Response.Write("State: " + generalInnerElement.Element("state").Value + "
") Response.Write("Shipping Taxable: " + shipping.ToString() + "
") End If End Sub End Class

 

Breakout: sample header for NewASP.NET

		<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="mssql-breakout.vb" Inherits="VBExtended" %;>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

		
Breakout: mssql-breakout.vb

		Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Xml

Partial Class VBExtended
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strZip As String = "90210"
        'sample zip code must be between 90001 and 90999
        Dim strUser As String = "sample"
        Dim strPassword As String = "password"
        Dim url As String
        url = "https://api.zip2tax.com/TaxRate-USA.xml?username=" & strUser & "&password=" & strPassword & "&zip=" & strZip

        Dim request As HttpWebRequest
        Dim http_response As HttpWebResponse = Nothing

        'Create the web request  
        request = DirectCast(WebRequest.Create(url), HttpWebRequest)

        ' Get response  
        http_response = DirectCast(request.GetResponse(), HttpWebResponse)

        ' Get the response stream into a reader  
        Dim reader As StreamReader = New StreamReader(http_response.GetResponseStream(), Encoding.UTF8)

        Dim str As String = reader.ReadLine()

        Dim doc As XDocument = XDocument.Parse(str)
        Dim generalElement As XElement = doc.Element("z2tLookup").Element("errorInfo")

        Dim errorInfo As String = generalElement.Element("errorMessage").Value

        If errorInfo = "Success" Then

            Dim generalInnerElement As XElement =
                    doc.Element("z2tLookup").Element("addressInfo").Element("addresses").Element("address")

            Dim generalInnerTaxElement As XElement =
                generalInnerElement.Element("salesTax").Element("rateInfo")

            Dim generalInnerNotesElement As XElement =
                    generalInnerElement.Element("notes")

            Dim generalInnerRateElement As XElement =
                    generalInnerTaxElement.Element("rateDetails")

            Dim childElement As XElement
            Dim shipping As Integer = 0

            For Each childElement In generalInnerNotesElement.Descendants("noteDetail").Elements("note")

                If childElement.Value.ToString().Equals("Shipping charges are not taxable") Then

                    shipping = 1
                End If
            Next

            Response.Write("Zip Code: " + generalInnerElement.Element("zipCode").Value + "
") Response.Write("Sales Tax Rate: " + generalInnerTaxElement.Element("taxRate").Value + "
") Response.Write("Post Office City: " + generalInnerElement.Element("place").Value + "
") Response.Write("County: " + generalInnerElement.Element("county").Value + "
") Response.Write("State: " + generalInnerElement.Element("state").Value + "
") Response.Write("Shipping Taxable: " + shipping.ToString() + "
") Dim index As Integer = 0 For Each childElement In generalInnerRateElement.Descendants("rateDetail").Elements("taxRate") If index = 0 Then Response.Write("Sales Tax Rate State: " + childElement.Value.ToString() + "
") index = index + 1 ElseIf index = 1 Then Response.Write("Sales Tax Rate County: " + childElement.Value.ToString() + "
") index = index + 1 ElseIf index = 2 Then Response.Write("Sales Tax Rate City: " + childElement.Value.ToString() + "
") index = index + 1 ElseIf index = 3 Then Response.Write("Sales Tax Rate Special: " + childElement.Value.ToString() + "
") index = index + 1 End If Next Response.Write("Sales Tax Reporting_Code Total: " + "(n/a)" + "
") Response.Write("Sales Tax Reporting_Code State: " + "(n/a)" + "
") index = 0 For Each childElement In generalInnerRateElement.Descendants("rateDetail").Elements("jurisdictionCode") If index = 1 Then Response.Write("Sales Tax Reporting_Code County: " + childElement.Value.ToString() + "
") index = index + 1 End If Next Response.Write("Sales Tax Reporting_Code City: " + "(n/a)" + "
") Response.Write("Sales Tax Reporting_Code Special: " + "(n/a)" + "
") End If End Sub End Class

 

Sample ZIP Code Range

In the sample ZIP code range (90001 - 90999), the following ZIP codes contain multiple tax rates; which can be used to test the return of multiple rows:

  • 90304
  • 90601
  • 90631
Terms of service: At Zip2Tax, your security and confidentiality are our highest priority. We will never share your e-mail address or any other personally identifiable information (PII) with any other person or company. Please review our full privacy policy. License: Please refer to our end user license agreement. Cancellation and Refund Policy: Please refer to our refund policy prior to purchase.
Copyright © 2006-2018 Zip2Tax, LLC. All rights reserved. Zip2Tax and Spot-On™ are trademarks of Zip2Tax, LLC, reg. pending U.S. Pat. & Tm. Off., S/N 86/613550 and 86/613547.

Quantcast
Quantcast
IP Address: 34.204.52.4
JavaScript: Fail
loggedd: none
WSPhilly03/DBPhilly03/CCProd

Don't have an account?

Sign up for a free trial here
If you do not have an account, please provide the information below.


Already have an account?
Free trial close Thank You!

Please check your email for your Username and Password.

If you do not receive your email shortly, check your SPAM folder for mail from "info@Zip2Tax.com" or call us at 1-866-492-8494, Monday - Friday 8am - 5pm EST.
Zip2Tax Password recovery close Password recovery

Please provide your registered email to verify and send password.



submit