List of usage examples for javax.net.ssl HttpsURLConnection addRequestProperty
public void addRequestProperty(String key, String value)
From source file:org.liberty.android.fantastischmemo.downloader.google.WorksheetFactory.java
public static void deleteWorksheet(Spreadsheet spreadsheet, Worksheet worksheet, String authToken) throws Exception { String requestUrl = "https://spreadsheets.google.com/feeds/worksheets/" + spreadsheet.getId() + "/private/full/" + worksheet.getId() + "/0" + "?access_token=" + authToken; URL url = new URL(requestUrl); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setRequestMethod("DELETE"); conn.addRequestProperty("If-Match", "*"); if (conn.getResponseCode() / 100 >= 3) { String s = new String(IOUtils.toByteArray(conn.getErrorStream())); throw new Exception(s); }/*ww w . jav a 2 s .c o m*/ }
From source file:org.liberty.android.fantastischmemo.downloader.google.WorksheetFactory.java
public static Worksheet createWorksheet(Spreadsheet spreadsheet, String title, int row, int col, String authToken) throws Exception { URL url = new URL("https://spreadsheets.google.com/feeds/worksheets/" + spreadsheet.getId() + "/private/full?access_token=" + authToken); String payload = "<entry xmlns=\"http://www.w3.org/2005/Atom\"" + " xmlns:gs=\"http://schemas.google.com/spreadsheets/2006\">" + "<title>" + URLEncoder.encode(title, "UTF-8") + "</title>" + "<gs:rowCount>" + row + "</gs:rowCount>" + "<gs:colCount>" + col + "</gs:colCount>" + "</entry>"; HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoInput(true);//from w ww . j av a2 s. co m conn.setDoOutput(true); conn.addRequestProperty("Content-Type", "application/atom+xml"); conn.addRequestProperty("Content-Length", "" + payload.getBytes("UTF-8").length); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(payload); out.close(); if (conn.getResponseCode() / 100 >= 3) { String s = new String(IOUtils.toByteArray(conn.getErrorStream())); throw new Exception(s); } List<Worksheet> worksheets = getWorksheets(spreadsheet, authToken); for (Worksheet worksheet : worksheets) { if (title.equals(worksheet.getTitle())) { return worksheet; } } throw new IllegalStateException("Worksheet lookup failed. Worksheet is not created properly."); }
From source file:org.liberty.android.fantastischmemo.downloader.google.CellsFactory.java
public static void uploadCells(Spreadsheet spreadsheet, Worksheet worksheet, Cells cells, String authToken) throws IOException { URL url = new URL("https://spreadsheets.google.com/feeds/cells/" + spreadsheet.getId() + "/" + worksheet.getId() + "/private/full/batch?access_token=" + authToken); String urlPrefix = "https://spreadsheets.google.com/feeds/cells/" + spreadsheet.getId() + "/" + worksheet.getId() + "/private/full"; for (int r = 0; r < cells.getRowCounts(); r += MAX_BATCH_SIZE) { int upBound = Math.min(r + MAX_BATCH_SIZE, cells.getRowCounts()); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoInput(true);//from w w w.j av a 2s . co m conn.setDoOutput(true); conn.addRequestProperty("Content-Type", "application/atom+xml"); //conn.addRequestProperty("Content-Length", "" + payload.length()); conn.addRequestProperty("If-Match", "*"); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); StringBuilder head = new StringBuilder(); head.append("<?xml version='1.0' encoding='UTF-8'?>"); head.append( "<feed xmlns=\"http://www.w3.org/2005/Atom\" xmlns:batch=\"http://schemas.google.com/gdata/batch\" xmlns:gs=\"http://schemas.google.com/spreadsheets/2006\">"); head.append("<id>" + urlPrefix + "</id>"); out.write(head.toString()); for (int i = r; i < upBound; i++) { List<String> row = cells.getRow(i); for (int j = 0; j < row.size(); j++) { out.write(getEntryToRequesetString(urlPrefix, i + 1, j + 1, row.get(j))); } } out.write("</feed>"); out.close(); if (conn.getResponseCode() / 100 >= 3) { String s = new String(IOUtils.toByteArray(conn.getErrorStream())); throw new IOException(s); } } }
From source file:org.liberty.android.fantastischmemo.downloader.quizlet.lib.java
public static boolean verifyAccessToken(final String[] accessTokens) throws Exception { final String TAG = "verfyAccessToken"; String token = accessTokens[0]; String userId = accessTokens[1]; try {/* w w w . ja v a2s. c o m*/ URL url1 = new URL(QUIZLET_API_ENDPOINT + "/users/" + userId); HttpsURLConnection conn = (HttpsURLConnection) url1.openConnection(); conn.addRequestProperty("Authorization", "Bearer " + String.format(token)); JsonReader s = new JsonReader(new InputStreamReader((conn.getInputStream()), "UTF-8")); s.beginObject(); while (s.hasNext()) { String name = s.nextName(); if ("error".equals(name)) { String error = s.nextString(); Log.e(TAG, "Token validation error: " + error); return false; } else { s.skipValue(); } } s.endObject(); s.close(); } catch (Exception e) { Log.i(TAG, "The saved access token is invalid", e); return false; } return true; }
From source file:org.liberty.android.fantastischmemo.downloader.quizlet.lib.java
public static String[] getAccessTokens(final String[] requests) throws Exception { final String TAG = "getAccesTokens"; String code = requests[0];// w w w . j ava 2s .c o m String clientIdAndSecret = QUIZLET_CLIENT_ID + ":" + QUIZLET_CLIENT_SECRET; String encodedClientIdAndSecret = Base64.encodeToString(clientIdAndSecret.getBytes(), 0); URL url1 = new URL("https://api.quizlet.com/oauth/token"); HttpsURLConnection conn = (HttpsURLConnection) url1.openConnection(); conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); // Add the Basic Authorization item conn.addRequestProperty("Authorization", "Basic " + encodedClientIdAndSecret); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); String payload = String.format("grant_type=%s&code=%s&redirect_uri=%s", URLEncoder.encode("authorization_code", "UTF-8"), URLEncoder.encode(code, "UTF-8"), URLEncoder.encode(Data.RedirectURI, "UTF-8")); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(payload); out.close(); if (conn.getResponseCode() / 100 >= 3) { Log.e(TAG, "Http response code: " + conn.getResponseCode() + " response message: " + conn.getResponseMessage()); JsonReader r = new JsonReader(new InputStreamReader(conn.getErrorStream(), "UTF-8")); String error = ""; r.beginObject(); while (r.hasNext()) { error += r.nextName() + r.nextString() + "\r\n"; } r.endObject(); r.close(); Log.e(TAG, "Error response for: " + url1 + " is " + error); throw new IOException("Response code: " + conn.getResponseCode()); } JsonReader s = new JsonReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); try { String accessToken = null; String userId = null; s.beginObject(); while (s.hasNext()) { String name = s.nextName(); if (name.equals("access_token")) { accessToken = s.nextString(); } else if (name.equals("user_id")) { userId = s.nextString(); } else { s.skipValue(); } } s.endObject(); s.close(); return new String[] { accessToken, userId }; } catch (Exception e) { // Throw out JSON exception. it is unlikely to happen throw new RuntimeException(e); } finally { conn.disconnect(); } }
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/*from w w w. j ava2s . co 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:org.liberty.android.fantastischmemo.downloader.quizlet.lib.java
/** * Make API call to Quizlet server with oauth * * @param url/*from w w w.j av a2s. c o m*/ * API call endpoint * @param authToken * oauth auth token * @return Response of API call * @throws IOException * If http response code is not 2xx */ public static InputStream makeApiCall(URL url, String authToken) throws IOException { HttpsURLConnection conn = null; try { conn = (HttpsURLConnection) url.openConnection(); if (authToken != null) { conn.addRequestProperty("Authorization", "Bearer " + authToken); } InputStream response = conn.getInputStream(); if (conn.getResponseCode() / 100 >= 3) { response = conn.getErrorStream(); } return response; } finally { //conn.disconnect(); } }
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;/*from w w w .j av a 2 s . c o 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); }
From source file:com.illusionaryone.BTTVAPIv2.java
@SuppressWarnings("UseSpecificCatch") private static JSONObject readJsonFromUrl(String urlAddress) { JSONObject jsonResult = new JSONObject("{}"); InputStream inputStream = null; URL urlRaw;/* www .ja va2 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.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:com.scaniatv.TipeeeStreamAPIv1.java
@SuppressWarnings("UseSpecificCatch") private static JSONObject readJsonFromUrl(String urlAddress) { JSONObject jsonResult = new JSONObject("{}"); InputStream inputStream = null; URL urlRaw;//from w ww.j ava 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); }