Code example for HttpsURLConnection

Methods: addRequestProperty, connect, disconnect, getOutputStream, getResponseCode, setDoOutput

1
		_password = password;
	} 
 
	private void writePost(HttpsURLConnection conn, String toPost)
			throws IOException { 
		conn.setDoOutput(true);
		OutputStream output = null;
		try { 
		     output = conn.getOutputStream();
		     output.write(toPost.getBytes());
		} finally { 
		     if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
		} 
	} 
 
	public HttpsURLConnection createConnection(URL url) throws IOException, Exception {
		HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
 
		if (_sessionId == null) {
			// basic authentication 
			String toBase64 = _username + ":" + _password;
			conn.addRequestProperty("Authorization", "basic " + new String(Base64.encodeBytes(toBase64.getBytes())));
		} else { 
			conn.addRequestProperty("Cookie", "sessionid=" + _sessionId);
		} 
		conn.addRequestProperty("User-Agent", "GPodder.net Account for Android");
 
		return conn;
	} 
 
	public boolean authenticate() { 
		verifyCurrentConfig(); 
 
		URL url;
		HttpsURLConnection conn = null;
		try { 
			url = new URL(_config.mygpo + "api/2/auth/" + _username + "/login.json");
			conn = createConnection(url);
			writePost(conn, " ");
 
			conn.connect();
 
			int code = conn.getResponseCode();
			if (code != 200)
				return false; 
 
			for (String val : conn.getHeaderFields().get("Set-Cookie")) {
				String[] data = val.split(";")[0].split("=");
				if (data[0].equals("sessionid"))
					_sessionId = data[1];
			} 
 
			return true; 
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally { 
			if (conn != null)
				conn.disconnect();
		} 
		return true; 
	} 
 
	public String getDeviceName() {
		verifyCurrentConfig(); 
 
		URL url;
		HttpsURLConnection conn = null;
		try { 
			url = new URL(_config.mygpo + "api/2/devices/" + _username + ".json");
			conn = createConnection(url);
			conn.connect();
 
			int code = conn.getResponseCode();
			if (code != 200)
				return null; 
 
			InputStream stream = conn.getInputStream();
			JsonReader reader = new JsonReader(new InputStreamReader(stream));
			try { 
				reader.beginArray();
				while (reader.hasNext()) {
					reader.beginObject();
					String id = null;
					String caption = null;
					while (reader.hasNext()) {
						String key = reader.nextName();
						if (key.equals("id"))
							id = reader.nextString();
						else if (key.equals("caption"))
							caption = reader.nextString();
						else 
							reader.skipValue();
					} 
 
					if (id != null && id.equals("podax"))
						return caption;
					reader.endObject();
				} 
				reader.endArray();
			} finally { 
				reader.close();
			} 
 
			return null; 
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally { 
			if (conn != null)
				conn.disconnect();
		} 
		return null; 
	} 
 
	public boolean setDeviceName(String deviceName) {
		verifyCurrentConfig(); 
 
		URL url;
		HttpsURLConnection conn = null;
		try { 
			url = new URL(_config.mygpo + "api/2/devices/" + _username + "/podax.json");
			conn = createConnection(url);
			writePost(conn, "{\"caption\": \"" + deviceName + "\", \"type\": \"mobile\"}");
			conn.connect();
 
			int code = conn.getResponseCode();
			if (code != 200)
				return false; 
			return true; 
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally { 
			if (conn != null)
				conn.disconnect();
		} 
		return true; 
	} 
 
	public class Changes {