USPS Address Validation Fail

2019-08-21 15:24发布

When validating an address, I get this error:

ex = {"Error Loading XML:  The following tags were not closed: AddressValidateRequest, Address, Address1.\r\n"}

or I get another error saying the address cannot be found. Is there a better way to validate this address?

Here is my URL:

http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML=<AddressValidateRequest USERID="402JMAWE3481"><Address ID="1"><Address1>123 Main St</Address1><Address2></Address2><City>Watertown</City><State>MA</State><Zip5>02472</Zip5><Zip4></Zip4></Address></AddressValidateRequest>

标签: c# xml usps
2条回答
淡お忘
2楼-- · 2019-08-21 15:37

According what I see on the error description, the problem could be that you need to remove \r\n from the xml before adding it to the url. Don't forget also to url encode it.

查看更多
放荡不羁爱自由
3楼-- · 2019-08-21 15:50

You actually need to HtmlEncode it and then UrlEncode it. This is becasue you're actually sending XML (which requires &amp; instead of &) but its a URL so it needs encoding to make each & into %26

Here's a complete working URL - you just need to put in your USERID.

http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML=<AddressValidateRequest USERID="123USERID567"><Address ID="1"><Address1></Address1><Address2>10051+Orr+%26amp%3b+Day+Rd</Address2><City>santa+fe+springs</City><State>ca</State><Zip5>90670</Zip5><Zip4></Zip4></Address></AddressValidateRequest>

You'll see it contains this funky looking string:

 10051+Orr+%26amp%3b+Day+Rd

Which I got by doing this :

 HttpUtility.UrlEncode(HttpUtility.HtmlEncode("10061 Orr & Day Rd"))

[This specific error I got back when I didn't encode properly was Error Loading XML: Whitespace is not allowed at this location]

查看更多
登录 后发表回答