Feb 18

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: , , , ,

Dec 08

Set the following system properties

  • http.proxyHost
  • http.proxyPort

java -Dhttp.proxyHost=proxy.objects.com.au -Dhttp.proxyPort=8080 au.com.objects.MyApplication

To do the same programatically use the System classess setProperty() method

   System.setProperty("http.proxyHost", proxyHostname);
   System.setProperty("http.proxyPort", proxyPort);

written by objects \\ tags: , ,

Dec 01

You can store the username of a visitor to your site in a cookie stored on their computer. You send the cookie with the http response, for example when they login.


Cookie cookie= new Cookie("username", username);  
cookie.setMaxAge(3600);  
response.addCookie(cookie);  

written by objects \\ tags: , ,