USPS地址验证失败

时间:2012-06-08 21:00:59

标签: c# xml usps

验证地址时,我收到此错误:

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

或者我得到另一个错误,说找不到地址。有没有更好的方法来验证这个地址?

这是我的网址:

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>

2 个答案:

答案 0 :(得分:3)

根据我在错误描述中看到的问题,问题可能是您需要先从xml中删除\r\n,然后再将其添加到网址。不要忘记url编码。

答案 1 :(得分:0)

你实际上需要HtmlEncode它然后UrlEncode它。 这是因为您实际上是在发送XML(需要&amp;而非&),但它是一个网址,因此需要进行编码才能使每个&成为%26

这是一个完整的工作网址 - 您只需要输入您的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>

你会看到它包含这个看起来很时髦的字符串:

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

我通过这样做得到了:

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

[当我没有正确编码时我得到的这个特定错误是Error Loading XML: Whitespace is not allowed at this location]