Code example for HttpsURLConnection

Methods: connect, getInputStream, getResponseCode, getResponseMessage, setInstanceFollowRedirects, setRequestMethod, setRequestProperty, setSSLSocketFactory

0
		url = new URL(apiServer.toString() + file);
		Log.d(TAG,"doGet : "+url.toString());
		HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
		this.setPassword();
		//con.setRequestProperty("X-Session-Id", session); 
		con.setRequestProperty("Accept", "application/json");
		con.setRequestProperty("Content-Type", "application/json");
		con.setInstanceFollowRedirects(true);
		con.setSSLSocketFactory(sslContext.getSocketFactory());
		try { 
			con.setRequestMethod("GET");
		} catch (ProtocolException e) {
			Log.e(TAG,"set get methods failed", e);
		} 
		 
		con.connect();
		Log.d(TAG,"done : "+url.toString());
		return con;
	} 
	 
	/** 
	 * Executes a simple HTTP-Get to the given URL. 
	 * 
	 * @param address the address 
	 * @return the connected HttpsUrlConnection for for this request 
	 * @throws IOException Signals that an I/O exception has occurred. 
	 */ 
	 
	public HttpsURLConnection doGetExt(String address) throws IOException{
		 
		URL url = null;
		url = new URL(address);
		Log.d(TAG,"doGet : "+url.toString());
		HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
		this.setPassword();
		//con.setRequestProperty("X-Session-Id", session); - not yet used 
//		con.setRequestProperty("Accept", "application/json"); 
//		con.setRequestProperty("Content-Type", "application/json"); 
		con.setInstanceFollowRedirects(true);
		con.setSSLSocketFactory(sslContext.getSocketFactory());
		try { 
			con.setRequestMethod("GET");
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block 
			Log.e(TAG,"set get methods failed");
		} 
		 
		con.connect();
		Log.e(TAG,"done : "+url.toString());
		return con;
	} 
	 
	/* (non-Javadoc) 
	 * @see tub.iosp.budcloand.framework.control.http.BCSession#getSubscribed() 
	 */ 
	@Override 
	public List<BCSubscribtion> getSubscribed() throws BCIOException, JSONException{
		// get the InputStream for the requested Endpoint and handle connection errors 
		InputStream in = handleGet("/subscribed",200);
		List<BCSubscribtion> result = BCJSONParser.parseSubscribtionList(IOHelper.streamToString(in), this.getUser());
		return result;
	} 
 
	/** 
	 * Handles an HTTP-post request to the given location on the apiserver. 
	 * 
	 * @param file the file 
	 * @param body the body 
	 * @param expected the expected response statuscode 
	 * @return the input stream for the response body 
	 * @throws BCIOException on IO errors 
	 */ 
	private InputStream handlePost(String file, byte[] body, int expected) throws BCIOException{
		int statusCode;
		String statusMessage;
		InputStream in;
		try{ 
			HttpsURLConnection con = doPost(file, body);
			statusCode = con.getResponseCode();
			Log.e(TAG,"statusCode : "+statusCode);
			in = con.getInputStream();
			statusMessage = con.getResponseMessage();
		} 
		catch(IOException e){
			// TODO 
			throw new BCIOException(e.toString());
		} 
		if(statusCode != 201) throw new StatusCodeException(statusCode, statusMessage, expected, "ok.");
		 
		return in;
	} 
	 
	/** 
	 * Handles a HTTP-get Request to the given file on the apiserver. 
	 * 
	 * @param file the file 
	 * @param expectedStatuscode the expected statuscode 
	 * @return an Inputstream with the servers responsebody 
	 * @throws BCIOException on IO errors 
	 */ 
	private InputStream handleGet(String  file, int expectedStatuscode) throws BCIOException{
		int statusCode = 0;
		String statusMessage = "";
		InputStream in = null;
		try { 
			HttpsURLConnection con = doGet(file);
			statusCode = con.getResponseCode();
			Log.e(TAG,"get status code:"+statusCode);
			in = con.getInputStream();
			Log.e(TAG,"get inputstream");
			statusMessage = con.getResponseMessage();
			 
		} catch (IOException e) {
			// TODO Auto-generated catch block 
			 
			Log.e(TAG,"handleGet wrong");