2

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>

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 documentation for tracking => https://www.usps.com/business/web-tools-apis/track-and-confirm.pdf

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();
}
Community
  • 1
  • 1
crazyim5
  • 706
  • 2
  • 9
  • 26

1 Answers1

2

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

Technical differences

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

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

Intended use differences

  • 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 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.

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.

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.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • Thank you for the reply. I understand the difference between GET and POST requests and in this case I would be making a POST request as I'm sending in the values. What I intended to ask was, should I just make a regular post request despite xml being part of the url? See my updated post. – crazyim5 May 06 '14 at 23:18
  • If you are doing a POST request, the data should not be part of the URL (and, alternatively, if the data is part of the URL, you should be doing a GET request). That said, some sites are sloppy or badly implemented and might expect you to send data both in the URL and the connection payload. As for the USPS, per the documentation you provided in your link, it seems it is expecting a GET request (even if I find a little strange fetching XML in an URL). – SJuan76 May 07 '14 at 08:01
  • Thanks for the input. Yeah it's a little strange that xml is requested as part of the url, as potentially this might lead to a very large url string but if their server can handle it, then should be okay. – crazyim5 May 07 '14 at 16:16