List of usage examples for javax.net.ssl HttpsURLConnection getInputStream
public InputStream getInputStream() throws IOException
From source file:ovh.tgrhavoc.aibot.RealmsUtil.java
private static String mcoapiGet(String address, YggdrasilSession session, ProxyData proxy) throws IOException { Proxy wrappedProxy = wrapProxy(proxy); HttpsURLConnection connection; if (wrappedProxy != null) connection = (HttpsURLConnection) new URL(address).openConnection(wrappedProxy); else/* ww w. j a va 2 s . c o m*/ connection = (HttpsURLConnection) new URL(address).openConnection(); connection.addRequestProperty("Cookie", "user=" + session.getUsername() + ";version=1.7.2;sid=token:" + session.getAccessToken().toString(16) + ":" + session.getSelectedProfile().getId()); connection.setUseCaches(false); connection.setDoOutput(true); connection.setDoOutput(true); connection.setRequestMethod("GET"); connection.connect(); InputStream in = connection.getInputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int read; while ((read = in.read(buffer)) != -1) out.write(buffer, 0, read); return new String(out.toByteArray()); }
From source file:com.illusionaryone.BTTVAPIv2.java
@SuppressWarnings("UseSpecificCatch") private static JSONObject readJsonFromUrl(String urlAddress) { JSONObject jsonResult = new JSONObject("{}"); InputStream inputStream = null; URL urlRaw;//from w w w . ja v a 2 s.com HttpsURLConnection urlConn; String jsonText = ""; try { urlRaw = new URL(urlAddress); urlConn = (HttpsURLConnection) urlRaw.openConnection(); urlConn.setDoInput(true); urlConn.setRequestMethod("GET"); urlConn.addRequestProperty("Content-Type", "application/json"); urlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36 PhantomBotJ/2015"); urlConn.connect(); if (urlConn.getResponseCode() == 200) { inputStream = urlConn.getInputStream(); } else { inputStream = urlConn.getErrorStream(); } BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8"))); jsonText = readAll(rd); jsonResult = new JSONObject(jsonText); fillJSONObject(jsonResult, true, "GET", urlAddress, urlConn.getResponseCode(), "", "", jsonText); } catch (JSONException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "JSONException", ex.getMessage(), jsonText); com.gmt2001.Console.err.printStackTrace(ex); } catch (NullPointerException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "NullPointerException", ex.getMessage(), ""); com.gmt2001.Console.err.printStackTrace(ex); } catch (MalformedURLException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "MalformedURLException", ex.getMessage(), ""); com.gmt2001.Console.err.printStackTrace(ex); } catch (SocketTimeoutException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "SocketTimeoutException", ex.getMessage(), ""); com.gmt2001.Console.err.printStackTrace(ex); } catch (IOException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "IOException", ex.getMessage(), ""); com.gmt2001.Console.err.printStackTrace(ex); } catch (Exception ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "Exception", ex.getMessage(), ""); com.gmt2001.Console.err.printStackTrace(ex); } finally { if (inputStream != null) try { inputStream.close(); } catch (IOException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "IOException", ex.getMessage(), ""); com.gmt2001.Console.err.printStackTrace(ex); } } return (jsonResult); }
From source file:Main.java
/** * * @param url - String//from w w w . j a v a2s . co m * @param reqParam refer POST method * @param accessToken - String * @return ArrayList<String> 0: responseBody * 1: responseCode */ public static ArrayList<String> getResponse(String url, String reqParam, String accessToken) { StringBuilder response = null; StringBuilder urlBuilder = null; BufferedReader in = null; HttpsURLConnection con = null; ArrayList<String> responseList = new ArrayList<String>(); int responseCode = -1; try { response = new StringBuilder(); urlBuilder = new StringBuilder(); urlBuilder.append(url); urlBuilder.append("?").append(reqParam); urlBuilder.append("&access_token=").append(accessToken); URL urlObj = new URL(urlBuilder.toString()); con = (HttpsURLConnection) urlObj.openConnection(); con.setRequestMethod("GET"); con.setDoInput(true); con.setDoOutput(false); con.setRequestProperty("Authorization: Bearer", accessToken); in = new BufferedReader(new InputStreamReader(con.getInputStream())); responseCode = con.getResponseCode(); String inputLine = ""; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } } catch (Exception e) { in = new BufferedReader(new InputStreamReader(con.getErrorStream())); String inputLine = ""; try { while ((inputLine = in.readLine()) != null) { response.append(inputLine); } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if (responseCode == -1) { } else { System.out.println(response.toString()); } } finally { try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } responseList.add(response.toString()); responseList.add(String.valueOf(responseCode)); return responseList; }
From source file:com.scaniatv.TipeeeStreamAPIv1.java
@SuppressWarnings("UseSpecificCatch") private static JSONObject readJsonFromUrl(String urlAddress) { JSONObject jsonResult = new JSONObject("{}"); InputStream inputStream = null; URL urlRaw;/*from w w w . j a v a 2 s. c om*/ HttpsURLConnection urlConn; String jsonText = ""; try { urlRaw = new URL(urlAddress); urlConn = (HttpsURLConnection) urlRaw.openConnection(); urlConn.setDoInput(true); urlConn.setRequestMethod("GET"); urlConn.addRequestProperty("Content-Type", "application/json"); urlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36 PhantomBotJ/2015"); urlConn.connect(); if (urlConn.getResponseCode() == 200) { inputStream = urlConn.getInputStream(); } else { inputStream = urlConn.getErrorStream(); } BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8"))); jsonText = readAll(rd); jsonResult = new JSONObject(jsonText); fillJSONObject(jsonResult, true, "GET", urlAddress, urlConn.getResponseCode(), "", "", jsonText); } catch (JSONException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "JSONException", ex.getMessage(), jsonText); com.gmt2001.Console.debug.println("TipeeeStreamAPIv1::readJsonFromUrl::Exception: " + ex.getMessage()); } catch (NullPointerException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "NullPointerException", ex.getMessage(), ""); com.gmt2001.Console.debug.println("TipeeeStreamAPIv1::readJsonFromUrl::Exception: " + ex.getMessage()); } catch (MalformedURLException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "MalformedURLException", ex.getMessage(), ""); com.gmt2001.Console.debug.println("TipeeeStreamAPIv1::readJsonFromUrl::Exception: " + ex.getMessage()); } catch (SocketTimeoutException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "SocketTimeoutException", ex.getMessage(), ""); com.gmt2001.Console.debug.println("TipeeeStreamAPIv1::readJsonFromUrl::Exception: " + ex.getMessage()); } catch (IOException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "IOException", ex.getMessage(), ""); com.gmt2001.Console.debug.println("TipeeeStreamAPIv1::readJsonFromUrl::Exception: " + ex.getMessage()); } catch (Exception ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "Exception", ex.getMessage(), ""); com.gmt2001.Console.debug.println("TipeeeStreamAPIv1::readJsonFromUrl::Exception: " + ex.getMessage()); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "IOException", ex.getMessage(), ""); com.gmt2001.Console.debug .println("TipeeeStreamAPIv1::readJsonFromUrl::Exception: " + ex.getMessage()); } } } return (jsonResult); }
From source file:com.illusionaryone.StreamTipAPI.java
@SuppressWarnings("UseSpecificCatch") private static JSONObject readJsonFromUrl(String urlAddress) { JSONObject jsonResult = new JSONObject("{}"); InputStream inputStream = null; URL urlRaw;/*from www . ja va 2 s. com*/ HttpsURLConnection urlConn; String jsonText = ""; try { urlRaw = new URL(urlAddress); urlConn = (HttpsURLConnection) urlRaw.openConnection(); urlConn.setDoInput(true); urlConn.setRequestMethod("GET"); urlConn.addRequestProperty("Content-Type", "application/json"); urlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36 PhantomBotJ/2015"); urlConn.connect(); if (urlConn.getResponseCode() == 200) { inputStream = urlConn.getInputStream(); } else { inputStream = urlConn.getErrorStream(); } BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8"))); jsonText = readAll(rd); jsonResult = new JSONObject(jsonText); fillJSONObject(jsonResult, true, "GET", urlAddress, urlConn.getResponseCode(), "", "", jsonText); } catch (JSONException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "JSONException", ex.getMessage(), jsonText); com.gmt2001.Console.debug.println("StreamTipAPI::readJsonFromUrl::Exception: " + ex.getMessage()); } catch (NullPointerException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "NullPointerException", ex.getMessage(), ""); com.gmt2001.Console.debug.println("StreamTipAPI::readJsonFromUrl::Exception: " + ex.getMessage()); } catch (MalformedURLException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "MalformedURLException", ex.getMessage(), ""); com.gmt2001.Console.debug.println("StreamTipAPI::readJsonFromUrl::Exception: " + ex.getMessage()); } catch (SocketTimeoutException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "SocketTimeoutException", ex.getMessage(), ""); com.gmt2001.Console.debug.println("StreamTipAPI::readJsonFromUrl::Exception: " + ex.getMessage()); } catch (IOException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "IOException", ex.getMessage(), ""); com.gmt2001.Console.debug.println("StreamTipAPI::readJsonFromUrl::Exception: " + ex.getMessage()); } catch (Exception ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "Exception", ex.getMessage(), ""); com.gmt2001.Console.debug.println("StreamTipAPI::readJsonFromUrl::Exception: " + ex.getMessage()); } finally { if (inputStream != null) try { inputStream.close(); } catch (IOException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "IOException", ex.getMessage(), ""); com.gmt2001.Console.debug .println("StreamTipAPI::readJsonFromUrl::Exception: " + ex.getMessage()); } } return (jsonResult); }
From source file:kltn.controller.StartController.java
public static void prinTest(AtmLocation atm) throws InterruptedException, org.json.simple.parser.ParseException, MalformedURLException, IOException { StringBuilder sb = new StringBuilder(); if (atm.getStreet() != null) { sb.append(atm.getStreet());//from w w w . ja v a 2s . c o m sb.append(","); } if (atm.getPrecinct() != null) { sb.append(atm.getPrecinct()); sb.append(","); } if (atm.getDistrict() != null) { sb.append(atm.getDistrict()); sb.append(","); } sb.append(atm.getProvinceCity()); sb.append(", Viet Nam"); String link = "https://maps.googleapis.com/maps/api/geocode/json?&key=AIzaSyALCgmmer3Cht-mFQiaJC9yoWdSqvfdAiM"; link = link + "&address=" + URLEncoder.encode(sb.toString()); URL url = new URL(link); HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection(); InputStream is = httpsCon.getInputStream(); StringWriter writer = new StringWriter(); IOUtils.copy(is, writer, "UTF-8"); String jsonString = writer.toString(); System.out.println(jsonString); System.out.println("----------------------------------------"); JSONParser parse = new JSONParser(); Object obj = parse.parse(jsonString); JSONObject jsonObject = (JSONObject) obj; if (!jsonObject.get("status").toString().toLowerCase().trim().equals("over_query_limit")) { JSONArray resultArray = (JSONArray) jsonObject.get("results"); // if (resultArray.size() == 1) { int countMatch = 0; for (Object resultO : resultArray) { JSONObject resultObject = (JSONObject) resultO; JSONArray addressArray = (JSONArray) resultObject.get("address_components"); String street_number = null; String route = null; String precinct = null; String district = null; String province = null; for (Object addressObj : addressArray) { JSONObject addressJsonObj = (JSONObject) addressObj; if (addressJsonObj.get("types") != null) { if (addressJsonObj.get("types").toString().contains("street_number")) { street_number = addressJsonObj.get("long_name").toString(); } else if (addressJsonObj.get("types").toString().contains("route")) { route = addressJsonObj.get("long_name").toString(); } else if (addressJsonObj.get("types").toString().contains("sublocality_level_1")) { precinct = addressJsonObj.get("long_name").toString(); } else if (addressJsonObj.get("types").toString().contains("administrative_area_level_2")) { district = addressJsonObj.get("long_name").toString(); } else if (addressJsonObj.get("types").toString().contains("administrative_area_level_1")) { province = addressJsonObj.get("long_name").toString(); } } } if (street_number != null && route != null) { if (atm.getFulladdress().toLowerCase().contains(street_number.toLowerCase()) && atm.getFulladdress().toLowerCase().contains(route.toLowerCase())) { countMatch++; // JSONArray geoArr = (JSONArray) resultObject.get("geometry"); JSONObject geoJsonObj = (JSONObject) resultObject.get("geometry"); // JSONArray locationArr = (JSONArray) geoJsonObj.get("location"); JSONObject locationJsonObj = (JSONObject) geoJsonObj.get("location"); atm.setLatd(locationJsonObj.get("lat").toString()); atm.setLongd(locationJsonObj.get("lng").toString()); } } } if (countMatch == 0) { atm.setStandardlization('3'); } // } System.out.println("============================================================================="); Thread.sleep(300); } else { System.out.println("Reach API LIMIT"); } atm.print(); }
From source file:com.tohours.imo.util.TohoursUtils.java
/** * /*from w w w . ja va 2 s . co m*/ * @param path * @param charsetName * @param param * @return * @throws IOException */ public static String httpsPost(String path, String param, String charsetName) throws IOException { String rv = null; URL url = null; HttpsURLConnection conn = null; InputStream input = null; try { url = new URL(path); conn = (HttpsURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "plain/text"); conn.setRequestProperty("User-Agent", "Tohours Project"); OutputStream os = conn.getOutputStream(); os.write(param.getBytes(charsetName)); os.flush(); os.close(); input = conn.getInputStream(); rv = TohoursUtils.inputStream2String(input, charsetName); } finally { if (input != null) { input.close(); } } return rv; }
From source file:dictinsight.utils.io.HttpUtils.java
/** * https??post/*from w w w.ja v a2s .c o m*/ * @param url * @param param * @return post? */ public static String httpsPostData(String url, String param) { class DefaultTrustManager implements X509TrustManager { @Override public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } } BufferedOutputStream brOutStream = null; BufferedReader reader = null; try { SSLContext context = SSLContext.getInstance("SSL"); context.init(null, new TrustManager[] { new DefaultTrustManager() }, new SecureRandom()); HttpsURLConnection connection = (HttpsURLConnection) (new URL(url)).openConnection(); connection.setSSLSocketFactory(context.getSocketFactory()); connection.setRequestMethod("POST"); connection.setRequestProperty("Proxy-Connection", "Keep-Alive"); connection.setDoInput(true); connection.setDoOutput(true); connection.setConnectTimeout(1000 * 15); brOutStream = new BufferedOutputStream(connection.getOutputStream()); brOutStream.write(param.getBytes()); brOutStream.flush(); reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String responseContent = ""; String line = reader.readLine(); while (line != null) { responseContent += line; line = reader.readLine(); } return responseContent; } catch (Exception e) { e.printStackTrace(); } finally { try { if (brOutStream != null) brOutStream.close(); if (reader != null) reader.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
From source file:org.wso2.carbon.automation.test.utils.http.client.HttpsURLConnectionClient.java
public static HttpsResponse getRequest(String Uri, String requestParameters) throws IOException { if (Uri.startsWith("https://")) { String urlStr = Uri;/*from ww w .j a v a 2 s .c o m*/ if (requestParameters != null && requestParameters.length() > 0) { urlStr += "?" + requestParameters; } URL url = new URL(urlStr); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setDoOutput(true); conn.setHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }); conn.setReadTimeout(30000); conn.connect(); // Get the response 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); } } catch (FileNotFoundException ignored) { } catch (IOException ignored) { } finally { if (rd != null) { rd.close(); } conn.disconnect(); } return new HttpsResponse(sb.toString(), conn.getResponseCode()); } return null; }
From source file:com.illusionaryone.TwitchTMIv1.java
@SuppressWarnings("UseSpecificCatch") private static JSONObject readJsonFromUrl(String urlAddress, boolean isArray) { JSONObject jsonResult = new JSONObject("{}"); InputStream inputStream = null; URL urlRaw;/* w w w .java2 s. co m*/ HttpsURLConnection urlConn; String jsonText = ""; try { urlRaw = new URL(urlAddress); urlConn = (HttpsURLConnection) urlRaw.openConnection(); urlConn.setDoInput(true); urlConn.setRequestMethod("GET"); urlConn.addRequestProperty("Content-Type", "application/json"); urlConn.addRequestProperty("Accept", "application/vnd.github.v3+json"); urlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36 PhantomBotJ/2015"); urlConn.connect(); if (urlConn.getResponseCode() == 200) { inputStream = urlConn.getInputStream(); } else { inputStream = urlConn.getErrorStream(); } BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8"))); jsonText = readAll(rd); if (isArray) { jsonResult = new JSONObject("{ \"array\": " + jsonText + " }"); } else { jsonResult = new JSONObject(jsonText); } fillJSONObject(jsonResult, true, "GET", urlAddress, urlConn.getResponseCode(), "", "", jsonText); } catch (JSONException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "JSONException", ex.getMessage(), jsonText); com.gmt2001.Console.err.println("TwitchTMIv1::readJsonFromUrl::Exception: " + ex.getMessage()); } catch (NullPointerException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "NullPointerException", ex.getMessage(), ""); com.gmt2001.Console.err.println("TwitchTMIv1::readJsonFromUrl::Exception: " + ex.getMessage()); } catch (MalformedURLException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "MalformedURLException", ex.getMessage(), ""); com.gmt2001.Console.err.println("TwitchTMIv1::readJsonFromUrl::Exception: " + ex.getMessage()); } catch (SocketTimeoutException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "SocketTimeoutException", ex.getMessage(), ""); com.gmt2001.Console.err.println("TwitchTMIv1::readJsonFromUrl::Exception: " + ex.getMessage()); } catch (IOException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "IOException", ex.getMessage(), ""); com.gmt2001.Console.err.println("TwitchTMIv1::readJsonFromUrl::Exception: " + ex.getMessage()); } catch (Exception ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "Exception", ex.getMessage(), ""); com.gmt2001.Console.err.println("TwitchTMIv1::readJsonFromUrl::Exception: " + ex.getMessage()); } finally { if (inputStream != null) try { inputStream.close(); } catch (IOException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "IOException", ex.getMessage(), ""); com.gmt2001.Console.err.println("TwitchTMIv1::readJsonFromUrl::Exception: " + ex.getMessage()); } } return (jsonResult); }