Code example for HttpsURLConnection

Methods: getOutputStream, getResponseCode, getResponseMessage, setDoInput, setDoOutput, setFixedLengthStreamingMode, setRequestMethod, setRequestProperty

0
 
			URL url = new URL(API_OAUTH2_TOKEN_URL);
			HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
			//conn.setReadTimeout(10000 /* milliseconds */); 
			//conn.setConnectTimeout(15000 /* milliseconds */);			 
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
			conn.setRequestProperty("Authorization", "Basic " + credentials);			
			conn.setFixedLengthStreamingMode(body.getBytes().length);
 
			//Send request 
			OutputStream out = new BufferedOutputStream(conn.getOutputStream());
			out.write(body.getBytes("UTF-8"));
			out.close ();
 
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {			
				//Get Response	 
				InputStream is = new BufferedInputStream(conn.getInputStream());
 
				//JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8")); 
			} 
			else { 
				Log.e(LOG_TAG, "Http response code: " + conn.getResponseCode() + ", " + conn.getResponseMessage());
			} 
 
 
		} catch (UnsupportedEncodingException e) {
			Log.e(LOG_TAG, "Failed to encode credentials");