我想构建一个访问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);更多推荐
发布评论