To send a HTTP GET request using Java can be done with the URL class. The openStream() method will send the GET request and return an input stream that can be used to read the HTTP response.
Wrapping the stream in a BufferedInputStream can be done to improve the I/O performance.
The ByteArrayOutputStream makes it easy to write the contents of the response to a byte array as shown in the following example.
URL url = new URL("http://www.objects.com.au/services/sherpa.html");
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
written by objects
\\ tags: ByteArrayOutputStream, GET, http, url, URLConnection
By default the RestDataSource only supports static url’s. If your backend requires dynamic url’s then you need to subclass RestDataSource and override transformRequest(). In transformRequest() you can set the actionURL property as required by your backend server.
// Following example shows how to append
// the entities if to the static update url
final RestDataSource restDataSource = new RestDataSource()
{
@Override
public Object transformRequest(DSRequest dsRequest)
{
if (dsRequest.getOperationType().equals(DSOperationType.UPDATE))
{
int id = JSOHelper.getAttributeAsInt(
dsRequest.getData(), "id"));
dsRequest.setActionURL(
getUpdateDataURL()+"/"+id;
}
return super.transformRequest(dsRequest);
}
};
written by objects
\\ tags: REST, RestDataSource, SmartGWT, url
URL parameters require the parameter values (and names) to be appropriately encoded. You can use the URLEncoder class to do this yourself, or the and tags can be used to handle it for you.
<c:url value="mypage.jsp" var="myUrl">
<c:param name="nameParam" value="${name}" />
<c:param name="ageParam" value="${age}" />
<c:param name="genderParam" value="${gender}" />
</c:url>
Click <a href='<c:out value="${myUrl}"/>'>here</a>
written by objects
\\ tags: encoding, parameter, url, URLEncoder
Recent Comments