1

I have the following code in Perl which posts to my HTTP Servlet, calling an API with some XML data.

my $my_hash = {
'env.adapterName' => "DefaultAdapter",
'env.systemName'  => "DefaultSystem",
'env.userId' => "admin",
'env.progId' => "PerlHttpTest",
InteropApiName => $apiName,
InteropApiData  => $xmlData
};

my $res = $ua->request(POST  'http://hostname/interop/InteropHttpServlet', $my_hash);

I would like to do this in Java but I am struggling. Could someone please point me in the right direction? I want to post data to my servlet as above and get the response back (it will be XML).

Matt
  • 2,163
  • 3
  • 26
  • 41

1 Answers1

3

The standard Java SE API offers you the java.net.URLConnection to fire and handle HTTP requests.

String query = "env.adapterName=DefaultAdapter"
    + "&env.systemName=DefaultSystem"
    + "&env.userId=admin"
    + "&env.progId=PerlHttpTest";
    + "&" + URLEncoder.encode(interopApiName, "UTF-8") + "=" + URLEncoder.encode(apiName, "UTF-8")
    + "&" + URLEncoder.encode(interopApiData, "UTF-8") + "=" + URLEncoder.encode(xmlData, "UTF-8");

URLConnection connection = new URL("http://hostname/interop/InteropHttpServlet").openConnection();
connection.setDoOutput(true); // Triggers POST.
connection.getOutputStream().write(query.getBytes("UTF-8"));

InputStream response = connection.getInputStream();
// ...

There exist 3rd party API's which makes this all little easier, like Apache HttpComponents Client.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • And how do you the equivalent of Perl’s `$response->decoded_content()` with what you get back? I hate all this manual encoding and decoding. Very error prone. – tchrist Aug 30 '11 at 13:28
  • @tchrist: What exactly needs to be decoded? The response body based on the `charset` in `Content-Type` header? You can also just go ahead with `HttpClient` to minimize this kind of boilerplate. Or Jsoup if all you want is to parse HTML. – BalusC Aug 30 '11 at 13:29
  • Ah, `HttpClient` is the right answer then. Thanks. I wouldn’t have guessed from `new URL`. ——— The content that comes back with the response is always in some encoding. It’s in the HTTP headers. If you just called `->content()` it will be raw encoded bytes, but if you call `->decoded_content()`, it will be a Unicode string no matter whether it was originally Latin1, CP1252, UTF-8, etc. That way you can use it in your program. Java also has a unified internal string rep, so to use the content you need to decode it according to the server-specified encoding. – tchrist Aug 30 '11 at 13:32
  • If the server specified a `charset` in the `Content-Type` header, use it, else use whatever you think is the best. See also the point "HTTP response encoding" in the *See also* link for a concrete example. Note that some specific content types like `application/xml` and `application/json` already implies UTF-8. – BalusC Aug 30 '11 at 13:34
  • I’m just not used to having to figure it out on my own. That’s why there’s a `decoded_content` method that produces an automatically decoded string instead of the raw `content` in undecoded bytes. With an internally contained encoding as in XML it is not so bad but with something like HTML where the server’s `Content-Type` takes priority it is critical to have or you can’t process the HTML. – tchrist Aug 30 '11 at 13:35
  • That's why HttpClient exist :) The `URLConnection` is just a barebones API (like everything in Java; if you want convenience for a specific task, look for a library, fortunately there are many choices in Java). – BalusC Aug 30 '11 at 13:36
  • Thanks all for the help. All your responses are very helpful. The standard Java APIs work well for me in my situation. – Matt Aug 30 '11 at 13:40
  • You might be interested in how rich the API is for the Perl [LWP::UserAgent](http://search.cpan.org/perldoc?LWP::UserAgent) and associated [HTTP::Response](http://search.cpan.org/perldoc?HTTP::Response) classes, the ones that the OP is using. Together they allow for some really high-level and user-friendly interaction. I assume `HttpClient` is more like these? – tchrist Aug 30 '11 at 13:43
  • @tchrist: I'm not familiar with Perl. However, I can tell that Perl (and PHP and like) is not really comparable with standard Java SE. Perl/PHP/Ruby/etc have practically "anything" builtin targeted to web developers. Java SE not. It just offers the basic core functionality which you can build further on (or look add-on libraries for). – BalusC Aug 30 '11 at 13:46