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 🙂
The scenario is as below. I get a request on my server, do some
Share
Preparing
We first need to know at least the URL and the
charset. The parameters are optional and depends on the functional requirements.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
charsetusingURLEncoder#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.
Any query string should be concatenated to the URL using ?. The
Accept-Charsetheader may hint the server what encoding the parameters are in. If you don’t send any query string, then you can leave theAccept-Charsetheader away. If you don’t need to set any headers, then you can even use theURL#openStream()shortcut method.Either way, if the other side is a
HttpServlet, then itsdoGet()method will be called and the parameters will be available byHttpServletRequest#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 typeapplication/x-www-form-urlencodedwherein the query string is written to the request body.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
URLConnectiontoHttpURLConnectionand use itsHttpURLConnection#setRequestMethod()instead. But if you’re trying to use the connection for output you still need to setURLConnection#setDoOutput()to true.Either way, if the other side is a
HttpServlet, then itsdoPost()method will be called and the parameters will be available byHttpServletRequest#getParameter().By the way Its almost a copy paste from following question
Using java.net.URLConnection to fire and handle HTTP requests