use of javax.net.ssl.HttpsURLConnection in project ballerina by ballerina-lang.
the class HttpsClientRequest method doGet.
/**
* Sends an HTTP GET request to a url.
*
* @param requestUrl - The URL of the service. (Example: "http://www.yahoo.com/search?params=value")
* @param headers - http request header map
* @return - HttpResponse from the end point
* @throws IOException If an error occurs while sending the GET request
*/
public static HttpResponse doGet(String requestUrl, Map<String, String> headers, String serverHome) throws IOException {
HttpsURLConnection conn = null;
HttpResponse httpResponse;
try {
conn = getURLConnection(requestUrl, serverHome);
// setting request headers
for (Map.Entry<String, String> e : headers.entrySet()) {
conn.setRequestProperty(e.getKey(), e.getValue());
}
conn.setRequestMethod(TestConstant.HTTP_METHOD_GET);
conn.connect();
StringBuilder sb = new StringBuilder();
BufferedReader rd = null;
try {
rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), Charset.defaultCharset()));
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
httpResponse = new HttpResponse(sb.toString(), conn.getResponseCode());
} catch (IOException ex) {
rd = new BufferedReader(new InputStreamReader(conn.getErrorStream(), Charset.defaultCharset()));
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
httpResponse = new HttpResponse(sb.toString(), conn.getResponseCode());
} finally {
if (rd != null) {
rd.close();
}
}
httpResponse.setHeaders(readHeaders(conn));
httpResponse.setResponseMessage(conn.getResponseMessage());
return httpResponse;
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
use of javax.net.ssl.HttpsURLConnection in project ballerina by ballerina-lang.
the class HttpsClientRequest method getURLConnection.
private static HttpsURLConnection getURLConnection(String requestUrl, String serverHome) throws IOException {
setSSlSystemProperties(serverHome);
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setReadTimeout(30000);
conn.setConnectTimeout(15000);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
return conn;
}
use of javax.net.ssl.HttpsURLConnection in project nifi-minifi by apache.
the class HttpConnector method get.
public HttpURLConnection get(String endpointPath, Map<String, List<String>> headers) throws ConfigurationProviderException {
String endpointUrl = baseUrl + endpointPath;
if (logger.isDebugEnabled()) {
logger.debug("Connecting to endpoint: " + endpointUrl);
}
URL url;
try {
url = new URL(endpointUrl);
} catch (MalformedURLException e) {
throw new ConfigurationProviderException("Malformed url " + endpointUrl, e);
}
HttpURLConnection httpURLConnection;
try {
if (proxy == null) {
httpURLConnection = (HttpURLConnection) url.openConnection();
} else {
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
}
if (sslContextFactory != null) {
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) httpURLConnection;
SSLContext sslContext = sslContextFactory.getSslContext();
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
httpsURLConnection.setSSLSocketFactory(socketFactory);
}
} catch (IOException e) {
throw new ConfigurationProviderException("Unable to connect to " + url, e);
}
if (proxyAuthorization != null) {
httpURLConnection.setRequestProperty("Proxy-Authorization", proxyAuthorization);
}
headers.forEach((s, strings) -> httpURLConnection.setRequestProperty(s, strings.stream().collect(Collectors.joining(","))));
return httpURLConnection;
}
use of javax.net.ssl.HttpsURLConnection in project nifi-minifi by apache.
the class HttpsStatusCodeHealthCheck method getHttpURLConnection.
public static HttpURLConnection getHttpURLConnection(String url, SSLSocketFactory sslSocketFactory, String proxyHostname, int proxyPort) throws IOException {
HttpsURLConnection httpURLConnection = (HttpsURLConnection) new URL(url).openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHostname, proxyPort)));
httpURLConnection.setSSLSocketFactory(sslSocketFactory);
return httpURLConnection;
}
use of javax.net.ssl.HttpsURLConnection in project MDM-Android-Agent by wso2-attic.
the class ServerUtilities method sendToServer.
public static Map<String, String> sendToServer(String epPostFix, Map<String, String> params, String option, Context context) throws IOException {
String response = null;
Map<String, String> response_params = new HashMap<String, String>();
String endpoint = CommonUtilities.SERVER_URL + epPostFix;
SharedPreferences mainPref = context.getSharedPreferences("com.mdm", Context.MODE_PRIVATE);
String ipSaved = mainPref.getString("ip", "");
if (ipSaved != null && ipSaved != "") {
endpoint = CommonUtilities.SERVER_PROTOCOL + ipSaved + ":" + CommonUtilities.SERVER_PORT + CommonUtilities.SERVER_APP_ENDPOINT + epPostFix;
}
URL url;
try {
url = new URL(endpoint);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("invalid url: " + endpoint);
}
StringBuilder bodyBuilder = new StringBuilder();
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String> param = iterator.next();
bodyBuilder.append(param.getKey()).append('=').append(param.getValue());
if (iterator.hasNext()) {
bodyBuilder.append('&');
}
}
String body = bodyBuilder.toString();
Log.v(TAG, "Posting '" + body + "' to " + url);
byte[] bytes = body.getBytes();
HttpURLConnection conn = null;
HttpsURLConnection sConn = null;
try {
if (url.getProtocol().toLowerCase().equals("https")) {
sConn = (HttpsURLConnection) url.openConnection();
sConn = getTrustedConnection(context, sConn);
sConn.setHostnameVerifier(WSO2MOBILE_HOST);
conn = sConn;
} else {
conn = (HttpURLConnection) url.openConnection();
}
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod(option);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Connection", "close");
// post the request
int status = 0;
Log.v("Check verb", option);
if (!option.equals("DELETE")) {
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.close();
// handle the response
status = conn.getResponseCode();
Log.v("Response Status", status + "");
InputStream inStream = conn.getInputStream();
response = inputStreamAsString(inStream);
response_params.put("response", response);
Log.v("Response Message", response);
response_params.put("status", String.valueOf(status));
} else {
status = Integer.valueOf(CommonUtilities.REQUEST_SUCCESSFUL);
}
if (status != Integer.valueOf(CommonUtilities.REQUEST_SUCCESSFUL) && status != Integer.valueOf(CommonUtilities.REGISTERATION_SUCCESSFUL)) {
throw new IOException("Post failed with error code " + status);
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (conn != null) {
conn.disconnect();
}
}
return response_params;
}
Aggregations