Code example for HttpsURLConnection

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

0
		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");
		} 
		if(statusCode != expectedStatuscode) throw new StatusCodeException(statusCode, statusMessage, expectedStatuscode, "ok.");
		//result = new BCSubscriptionList(IOHelper.streamToString(in)); 
		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 handleGetExt(String  file, int expectedStatuscode) throws BCIOException{
		int statusCode = 0;
		String statusMessage = "";
		InputStream in = null;
		try { 
			HttpsURLConnection con = doGetExt(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,e.toString()); 
			//throw new BCIOException(e.toString());