在Java中将xml作为URL请求的一部分发送 [英] Send xml as part of URL request in Java

查看:227
本文介绍了在Java中将xml作为URL请求的一部分发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个微不足道的问题,但我正在尝试向USPS发送网页请求,以根据我发送的跟踪号码获取包含跟踪信息的http帖子回复(或根据我的请求发送电子邮件回复)。文档说的XML需要追加为象下面

This might be a trivial question but I'm trying to send web request to USPS to get a http post response (or email response depending on my request) containing the tracking information based on the tracking number that I send in. The documentation says the xml needs to appended as part of the url like below

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请求之间有什么区别。有人可以帮我清理一下吗?

I saw there were 2 ways to make an xml request, one using HttpPost and the other URLConnection. I'm a bit thrown by how I go about this and I'm failing to appreciate what's the difference between appending xml in the url and a normal http request. Can someone please clear things up for me?

用于跟踪的USPS文档=>
https://www.usps.com/business/web-tools-apis/track-and-confirm.pdf

USPS documentation for tracking => https://www.usps.com/business/web-tools-apis/track-and-confirm.pdf

我读了这些相关的Stackoverflow帖子

Java:如何发送XML请求?

在java中发布XML请求

I read these related Stackoverflow posts
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();
}


推荐答案

HTTP请求可以使用一个几种方法,如POST,GET,DELETE,PUT ......这里我们谈谈POST和GET

An HTTP request can use one of several methods, like POST, GET, DELETE, PUT... Here we talk about POST and GET

技术差异


  • 使用GET,可以从URL中的参数中检索数据。

  • With GET, the data is retrieved from the parameters in the URL.

使用POST,从HTTP消息中传输的数据中检索数据。

With POST, the data is retrieved from the data transmitted inside the HTTP message.

预期用途差异


  • 当请求不会导致更改时(vg,在Google中搜索),将使用GET。由于您可以在没有副作用的情况下重复请求,因此数据位于URL中,可以存储在浏览器历史记录,收藏夹等中。

  • GET is intended to be used when the request does not cause a change (v.g., searching in Google). Since you can repeat the request without side effects, the data is in the URL and can be stored in the browser history, favorites, etc.

POST意图在执行更改时使用(vg发送电子邮件,进行在线购买)。相关数据不会与URL一起存储(如果你回到使用POST获得的页面,浏览器会多次显示弹出窗口,要求获得再次发送数据的权限。

POST is intended to use when you are performing a change (v.g. sending an e-mail, doing a on-line purchase). The data related is not stored with the URL (it is then that, if you go back to a page that was obtained using POST, the browser many times will show you a pop-up asking for permission to send the data again.

在实际使用中,区别并不是那么明确,特别是当数据太大时有时会使用POST( URL的长度有限。)此外,有时GET与POST的含义一起使用,因此数据可以显示为HTML链接。

In real usage, the distinction is not so clear cut, in particular POST is sometimes used when the data is too large (URLs have limited length). Also, sometimes GET is used with the meaning of POST so the data can be presented as an HTML link.

最后, URLConnection 是打开连接的基本API(您可以将其用作POST或GET请求,基于传递数据的方式或其他内容)和 HttpPost 只是用于创建POST请求的更高级API。如果你采用基本方法,请更好地使用 HttpURLConnection

Finally, URLConnection is the basic API for opening a connection (which you can use as a POST or GET request, based in how you pass the data, or something else) and HttpPost is just a higher level API for creating a POST request. If you go the basic way, use HttpURLConnection better.

这篇关于在Java中将xml作为URL请求的一部分发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆