4.2.6. HttpsURLConnection
Setting up an HTTPS connection
An HTTPS connection to a URL is set up as follows (Get request):
URL url = new URL("https:/servername/content?param=value")
HttpsURLConnection httpsCon = (HttpsURLConnection)
url.openConnection();
The following properties can be set additionally:
httpsCon.setDoOutput(true);
httpsCon.setUseCaches(false);
// set some cookies
httpsCon.setRequestProperty("Cookie",cookieString);
Caution: The following request is only sent when the HTTP response is requested:
int rc = httpsCon.getResponseCode();
String msg = httpsCon.getResponseMessage();
Reading sent HTML/XML pages
The sent HTML/XML pages are read as follows:
InputStream in = httpsCon.getInputStream();
BufferedReader result = new BufferedReader(new InputStreamReader(in));
Request body for POST request
The following request body is written for a POST request:
DataOutputStream out = new DataOutputStream( httpsCon.getOutputStream());
out.writeBytes("param1=value¶m2=value");
Note: The method setRequestMethod() of the connection has no effect on the type of request generated. If the request body is not defined, a GET request is sent, otherwise a POST request.