dcsimg

How do I control whether URLConnection uses the POST or GET method when sending data to an HTTP server?

David Gommeren

Since these methods are part of the HTTP protocol, they can only be used when the URL is an "http://" URL. In that case, you can cast the connection returned from URL.openConnection() to be a HttpURLConnection, which has a setRequestMethod() method which lets you select whether GET or POST is used. For example:

    if (connection instanceof HttpURLConnection)
        ((HttpURLConnection)connection).setRequestMethod("POST");

GET is used by default.