Code example for HttpsURLConnection

Methods: getErrorStream, getHeaderField, getInputStream, getOutputStream, getResponseCode, setDoOutput, setFixedLengthStreamingMode, setInstanceFollowRedirects, setRequestMethod

0
	} 
 
	private User handleLoginResponse(HttpsURLConnection connection)
			throws IOException, LoginException { 
 
		final int responseCode = connection.getResponseCode();
		switch (responseCode) {
		case HttpURLConnection.HTTP_OK:
			// OK, but means we had an error on login. 
			final BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(connection.getInputStream()), 2 << 11);
 
			try { 
				this.raiseLoginErrorFromResponseStream(bufferedReader);
			} finally { 
				bufferedReader.close();
			} 
			break; 
		case HttpURLConnection.HTTP_MOVED_TEMP:
			// Can mean we reached the maximum login attempts or the login 
			// did actually work. 
			final String location = connection.getHeaderField("Location");
 
			if (location.equals("/index.php?id=90")) {
				// The success page. 
				return this.retrieveUserInformation();
			} else { 
				// There could actually be other reasons for this to happen, 
				// but this is the most likely case. 
				Ln.w("Got a redirect on login to " + location);
				throw new LoginException("Your account was banned for an hour."); 
			} 
		case HttpURLConnection.HTTP_INTERNAL_ERROR:
			// Server error. The HttpURLConnection may provide additional 
			// information. 
			final InputStream error = connection.getErrorStream();
			final BufferedReader bufferedErrorReader = new BufferedReader(
				new InputStreamReader(error), 2 << 11
			); 
			try { 
				this.raiseLoginErrorFromServerErrorResponseStream(
						bufferedErrorReader);
			} finally { 
				bufferedErrorReader.close();
			} 
			break; 
		default: 
			// Throw exception below. 
		} 
 
		Ln.e("Login failed with code " + responseCode);
		throw new LoginException("The login failed for an " 
				+ " unknown reason."); 
	} 
 
	/** 
	 * In order to retrieve the current challenge, you got to call the 
	 * retrieveChallenge() method first. 
	 * 
	 * @param challenge 
	 * @return 
	 * @throws NoSuchAlgorithmException 
	 * @throws IOException 
	 */ 
	public User login(String challenge) throws IOException, LoginException {
 
		String passphrase = null;
		try { 
			passphrase = this.generatePassphrase(challenge);
		} catch (NoSuchAlgorithmException e) {
			Ln.e(e);
		} 
		Ln.d("Passphrase: " + passphrase);
 
		final HttpsURLConnection connection = this
				.buildConnection("community-login.html"); 
 
		// Setting method to POST per default. 
		connection.setDoOutput(true);
		// Implied by the above, but more explicit this way. 
		connection.setRequestMethod("POST");
		// This is false for 10, but apparently true at least for 14+. 
		connection.setInstanceFollowRedirects(false);
 
		final byte[] params = this.getLoginPostParameters(passphrase, challenge);
		Ln.d("Sending POST params for login " + new String(params));
 
		connection.setFixedLengthStreamingMode(params.length);
 
		final OutputStream out = connection.getOutputStream();
		 
		try { 
			out.write(params);
		} finally { 
			out.close();