Java tutorial
/******************************************************************************* * Copyright (c) 2017 Luke Collins and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the license * which accompanies this distribution * * Contributors: * Luke Collins *******************************************************************************/ package net.lukecollins.dev.github; import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.UnsupportedEncodingException; public class HttpUtils { public static final org.slf4j.Logger log = LoggerFactory.getLogger(HttpUtils.class); private static final String LOG_CONTEXT = "context"; /** * Get Response for Service. * @param service servicename * @return returns HttpResponse of Http GET */ public static HttpResponse getRequest(String service) { HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet(GhatConstantsUtil.URL + service); //Add token to header String token = GhatConstantsUtil.APIKEY + ":x-oauth-basic"; String authString = null; try { authString = "Basic " + Base64.encodeBase64String(token.getBytes("UTF-8")); } catch (UnsupportedEncodingException exp) { log.error(LOG_CONTEXT, exp); } httpGet.addHeader("Authorization", authString); HttpResponse response = null; try { response = httpClient.execute(httpGet); } catch (IOException exp) { log.error(LOG_CONTEXT, exp); } return response; } }