【发布时间】:2014-06-23 17:24:44
【问题描述】:
这可能是一个微不足道的问题,但我正在尝试向 USPS 发送网络请求,以获取包含基于我发送的跟踪号的跟踪信息的 http 发布响应(或电子邮件响应,具体取决于我的请求)。文档说 xml 需要作为 url 的一部分附加,如下所示
http://secure.shippingapis.com/ShippingAPITest.dll?API=TrackV2&XML=<PTSEmailRequest USERID="xxxxx"><TrackId>xxxxx</TrackId><RequestType>EN</RequestType></PTSEmailRequest>
我看到有两种方法可以发出 xml 请求,一种使用HttpPost,另一种使用URLConnection。我对我的处理方式有点失望,我无法理解在 url 中附加 xml 和正常的 http 请求之间有什么区别。有人可以帮我清理一下吗?
用于跟踪的 USPS 文档 => https://www.usps.com/business/web-tools-apis/track-and-confirm.pdf
我阅读了这些 Stackoverflow 相关帖子
Java: How to send a XML request?
posting XML request in java
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://secure.shippingapis.com/ShippingAPITest.dll");
List<String> params = new ArrayList<String>(2);
params.add(new BasicNameValuePair("API", "TrackV2"));
params.add(new BasicNameValuePair("XML", FuncTOGenerateXML()));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
//.....
// .....
instream.close();
}
【问题讨论】:
标签: java xml xmlhttprequest http-post usps