C
cj
This code might even be useful to some of you. I'm playing with USPS'
interface to validate addresses. How do I deal with the fact that not
all the elements are returned all the time? For instance this inquiry
does not return a firm name. If it doesn't exist it should be blank in
my processing but as is since it doesn't exist my code stops when trying
to update the text box.
Dim URL As String
Dim webReq As Net.HttpWebRequest
Dim webResp As Net.HttpWebResponse
Dim respStr As String
Dim startTime As DateTime
Dim finishTime As DateTime
Try
URL =
"http://testing.shippingapis.com/ShippingAPITest.dll?API=Verify&XML=<AddressValidateRequest%"
& _
"20USERID=""myuserid""><Address ID=""0""><Address1></Address1>" & _
"<Address2>6406 Ivy
Lane</Address2><City>Greenbelt</City><State>MD</State>" & _
"<Zip5></Zip5><Zip4></Zip4></Address></AddressValidateRequest>"
webReq = Net.HttpWebRequest.Create(URL)
startTime = Now
webResp = webReq.GetResponse
finishTime = Now
respStr = New IO.StreamReader(webResp.GetResponseStream).ReadToEnd
Catch ex As Exception
MessageBox.Show("ex.Message = " & ex.Message)
Finally
webResp.Close()
End Try
Dim doc As New Xml.XmlDocument
doc.LoadXml(respStr)
Dim rFirmName As Xml.XmlNodeList = doc.GetElementsByTagName("FirmName")
Dim rAddress1 As Xml.XmlNodeList = doc.GetElementsByTagName("Address1")
Dim rAddress2 As Xml.XmlNodeList = doc.GetElementsByTagName("Address2")
Dim rCity As Xml.XmlNodeList = doc.GetElementsByTagName("City")
Dim rState As Xml.XmlNodeList = doc.GetElementsByTagName("State")
Dim rZip5 As Xml.XmlNodeList = doc.GetElementsByTagName("Zip5")
Dim rZip4 As Xml.XmlNodeList = doc.GetElementsByTagName("Zip4")
Dim rReturnText As Xml.XmlNodeList = doc.GetElementsByTagName("ReturnText")
TextBox3.Text = rFirmName(1).InnerText & vbCrLf & _
rAddress1(1).InnerText & vbCrLf & _
rAddress2(1).InnerText & vbCrLf & _
rCity(1).InnerText & ", " & _
rState(1).InnerText & " " & _
rZip5(1).InnerText & "-" & _
rZip4(1).InnerText & vbCrLf & _
rReturnText(1).InnerText & vbCrLf & _
"Time: " & finishTime.Subtract(startTime).ToString
interface to validate addresses. How do I deal with the fact that not
all the elements are returned all the time? For instance this inquiry
does not return a firm name. If it doesn't exist it should be blank in
my processing but as is since it doesn't exist my code stops when trying
to update the text box.
Dim URL As String
Dim webReq As Net.HttpWebRequest
Dim webResp As Net.HttpWebResponse
Dim respStr As String
Dim startTime As DateTime
Dim finishTime As DateTime
Try
URL =
"http://testing.shippingapis.com/ShippingAPITest.dll?API=Verify&XML=<AddressValidateRequest%"
& _
"20USERID=""myuserid""><Address ID=""0""><Address1></Address1>" & _
"<Address2>6406 Ivy
Lane</Address2><City>Greenbelt</City><State>MD</State>" & _
"<Zip5></Zip5><Zip4></Zip4></Address></AddressValidateRequest>"
webReq = Net.HttpWebRequest.Create(URL)
startTime = Now
webResp = webReq.GetResponse
finishTime = Now
respStr = New IO.StreamReader(webResp.GetResponseStream).ReadToEnd
Catch ex As Exception
MessageBox.Show("ex.Message = " & ex.Message)
Finally
webResp.Close()
End Try
Dim doc As New Xml.XmlDocument
doc.LoadXml(respStr)
Dim rFirmName As Xml.XmlNodeList = doc.GetElementsByTagName("FirmName")
Dim rAddress1 As Xml.XmlNodeList = doc.GetElementsByTagName("Address1")
Dim rAddress2 As Xml.XmlNodeList = doc.GetElementsByTagName("Address2")
Dim rCity As Xml.XmlNodeList = doc.GetElementsByTagName("City")
Dim rState As Xml.XmlNodeList = doc.GetElementsByTagName("State")
Dim rZip5 As Xml.XmlNodeList = doc.GetElementsByTagName("Zip5")
Dim rZip4 As Xml.XmlNodeList = doc.GetElementsByTagName("Zip4")
Dim rReturnText As Xml.XmlNodeList = doc.GetElementsByTagName("ReturnText")
TextBox3.Text = rFirmName(1).InnerText & vbCrLf & _
rAddress1(1).InnerText & vbCrLf & _
rAddress2(1).InnerText & vbCrLf & _
rCity(1).InnerText & ", " & _
rState(1).InnerText & " " & _
rZip5(1).InnerText & "-" & _
rZip4(1).InnerText & vbCrLf & _
rReturnText(1).InnerText & vbCrLf & _
"Time: " & finishTime.Subtract(startTime).ToString