构建USPS Web服务请求(Build USPS web service request)

编程入门 行业动态 更新时间:2024-10-18 10:18:26
构建USPS Web服务请求(Build USPS web service request)

我想构建一个访问USPS地址Web服务的Web服务请求。 我遇到了以他们想要的格式构建请求URL字符串的问题。 我想要做的是将邮政编码放在一个变量中,以便它可以是动态的。 但USPS网络服务不接受我发送的URL字符串,我猜我的格式错误。

USPS期望的格式是:

<CityStateLookupRequest USERID=”xxxxxxxx”> <ZipCode ID="0"> <Zip5>90210</Zip5> </ZipCode> <ZipCode ID="1"> <Zip5>20770</Zip5> </ZipCode> </CityStateLookupRequest> https://servername/ShippingAPI.dll?API=CityStateLookup&XML=<CityStateLookupRe quest USERID="username">.......</CityStateLookupRequest>

这就是我尝试构建URL的方式:

WebRequest USPSReq = String.Format("http://production.shippingapis.com/ShippingAPI.dll?API=CityStateLookup&XML=CityStateLookupRequest&USERID=xxxxxxxx&ZipCode ID=0&Zip5=" + oZip);

如何构建此请求URL?

I want to build a web service request which accesses USPS's address web service. I am facing a problem with building the request URL string in the format they want. What i want to do is give the zip code in a variable so that it can be dynamic. But USPS web service isn't accepting the URL string I am sending, guess I am making a mistake with the format.

The format which USPS expects is:

<CityStateLookupRequest USERID=”xxxxxxxx”> <ZipCode ID="0"> <Zip5>90210</Zip5> </ZipCode> <ZipCode ID="1"> <Zip5>20770</Zip5> </ZipCode> </CityStateLookupRequest> https://servername/ShippingAPI.dll?API=CityStateLookup&XML=<CityStateLookupRe quest USERID="username">.......</CityStateLookupRequest>

This is how I am trying to build the URL:

WebRequest USPSReq = String.Format("http://production.shippingapis.com/ShippingAPI.dll?API=CityStateLookup&XML=CityStateLookupRequest&USERID=xxxxxxxx&ZipCode ID=0&Zip5=" + oZip);

How can I build this request URL?

最满意答案

只需使用您喜欢的XML API构建该XML。 例如:

XDocument requestXml = new XDocument( new XElement("CityStateLookupRequest", new XAttribute("USERID", userID), new XElement("ZipCode", new XAttribute("ID", "0"), new XElement("ZIP5", zip5))); var requestUrl = new UriBuilder("http://production.shippingapis.com/ShippingAPITest.dll"); requestUrl.Query = "API=CityStateLookup&XML=" + requestXml.ToString(); var request = WebRequest.Create(requestUrl.Uri);

Simply build that XML using your favorite XML API. For instance:

XDocument requestXml = new XDocument( new XElement("CityStateLookupRequest", new XAttribute("USERID", userID), new XElement("ZipCode", new XAttribute("ID", "0"), new XElement("ZIP5", zip5))); var requestUrl = new UriBuilder("http://production.shippingapis.com/ShippingAPITest.dll"); requestUrl.Query = "API=CityStateLookup&XML=" + requestXml.ToString(); var request = WebRequest.Create(requestUrl.Uri);

更多推荐

本文发布于:2023-07-26 00:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268427.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Build   Web   USPS   request   service

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!