Reputation: 11
The scenario is as below. I get a request on my server, do some processing on it and then I need to place request on another server based on my processing, then build response on bases of what I get from the remote server. Its to be done in JAVA Playframework 2.0 and I'm missing on part of sending request and getting response from remote server. Any help would be appreciated. Thanks :)
Upvotes: 0
Views: 4545
Reputation: 21564
To call a request from your Play server, you should use the WS API. It's easy to use, and also you can make remote calls in an asynchronous way:
public static Result feedTitle(String feedUrl) {
return async(
WS.url(feedUrl).get().map(
new Function<WS.Response, Result>() {
public Result apply(WS.Response response) {
return ok("Feed title:" + response.asJson().findPath("title"));
}
}
)
);
}
More infos in the Play documentation.
Upvotes: 3
Reputation: 969
Preparing
We first need to know at least the URL and the charset
. The parameters are optional and depends on the functional requirements.
String url = "http://example.com";
String charset = "UTF-8";
String param1 = "value1";
String param2 = "value2";
// ...
String query = String.format("param1=%s¶m2=%s",
URLEncoder.encode(param1, charset),
URLEncoder.encode(param2, charset));
The query parameters must be in name=value format and be concatenated by &. You would normally also URL-encode the query parameters with the specified charset
using URLEncoder#encode()
.
The String#format() is just for convenience. I prefer it when I would need the String concatenation operator + more than twice.
Firing an HTTP GET request with (optionally) query parameters:
It's a trivial task. It's the default request method.
URLConnection connection = new URL(url + "?" + query).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
// ...
Any query string should be concatenated to the URL using ?. The Accept-Charset
header may hint the server what encoding the parameters are in. If you don't send any query string, then you can leave the Accept-Charset
header away. If you don't need to set any headers, then you can even use the URL#openStream()
shortcut method.
InputStream response = new URL(url).openStream();
// ...
Either way, if the other side is a HttpServlet
, then its doGet()
method will be called and the parameters will be available by HttpServletRequest#getParameter()
.
Firing a HTTP POST request with query parameters:
Firing an HTTP POST request with query parameters:
Setting the URLConnection#setDoOutput()
to true implicitly sets the request method to POST. The standard HTTP POST as web froms do is of type application/x-www-form-urlencoded
wherein the query string is written to the request body.
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true); // Triggers POST.
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
OutputStream output = null;
try {
output = connection.getOutputStream();
output.write(query.getBytes(charset));
} finally {
if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
}
InputStream response = connection.getInputStream();
// ...
Note: whenever you'd like to submit a HTML form programmatically, don't forget to take the name=value pairs of any elements into the query string and of course also the name=value pair of the element which you'd like to "press" programmatically (because that's usually been used in the server side to distinguish if a button was pressed and if so, which one).
You can also cast the obtained URLConnection
to HttpURLConnection
and use its HttpURLConnection#setRequestMethod()
instead. But if you're trying to use the connection for output you still need to set URLConnection
#setDoOutput()
to true.
HttpURLConnection httpConnection = (HttpURLConnection) new URL(url).openConnection();
httpConnection.setRequestMethod("POST");
// ...
Either way, if the other side is a HttpServlet
, then its doPost()
method will be called and the parameters will be available by HttpServletRequest#getParameter()
.
By the way Its almost a copy paste from following question
Using java.net.URLConnection to fire and handle HTTP requests
Upvotes: 3