List of usage examples for javax.net.ssl HttpsURLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:org.schabi.newpipe.Downloader.java
/**Download the text file at the supplied URL as in download(String), * but set the HTTP header field "Accept-Language" to the supplied string. * @param siteUrl the URL of the text file to return the contents of * @param language the language (usually a 2-character code) to set as the preferred language * @return the contents of the specified text file*/ public static String download(String siteUrl, String language) { String ret = ""; try {//from w w w. ja va2 s . c om URL url = new URL(siteUrl); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); con.setRequestProperty("Accept-Language", language); ret = dl(con); } catch (Exception e) { e.printStackTrace(); } return ret; }
From source file:org.liberty.android.fantastischmemo.downloader.google.FolderFactory.java
public static void addDocumentToFolder(Document document, Folder folder, String authToken) throws XmlPullParserException, IOException { URL url = new URL("https://www.googleapis.com/drive/v2/files/" + document.getId() + "/parents"); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoInput(true);//from ww w .j a v a2 s. c o m conn.setDoOutput(true); conn.setRequestProperty("Authorization", "Bearer " + authToken); conn.setRequestProperty("Content-Type", "application/json"); String payload = "{\"id\":\"" + folder.getId() + "\"}"; conn.setRequestProperty("Content-Length", "" + payload.length()); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(conn.getOutputStream()); outputStreamWriter.write(payload); outputStreamWriter.close(); if (conn.getResponseCode() / 100 >= 3) { String s = new String(IOUtils.toByteArray(conn.getErrorStream())); throw new RuntimeException(s); } }
From source file:org.liberty.android.fantastischmemo.downloader.google.FolderFactory.java
public static Folder createFolder(String title, String authToken) throws XmlPullParserException, IOException { URL url = new URL("https://www.googleapis.com/drive/v2/files"); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoInput(true);/*from w ww. j a va 2 s.co m*/ conn.setDoOutput(true); conn.setRequestProperty("Authorization", "Bearer " + authToken); conn.setRequestProperty("Content-Type", "application/json"); // Used to calculate the content length of the multi part String payload = "{\"title\":\"" + title + "\",\"mimeType\":\"application/vnd.google-apps.folder\"}"; conn.setRequestProperty("Content-Length", "" + payload.length()); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(conn.getOutputStream()); outputStreamWriter.write(payload); outputStreamWriter.close(); if (conn.getResponseCode() / 100 >= 3) { String s = new String(IOUtils.toByteArray(conn.getErrorStream())); throw new RuntimeException(s); } return EntryFactory.getEntryFromDriveApi(Folder.class, conn.getInputStream()); }
From source file:tk.egsf.ddns.JSON_helper.java
public static String getJsonUrl(String URL, String Auth) { String ret = ""; String https_url = URL; URL url;//w w w . j a va 2 s .c o m try { TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted(X509Certificate[] certs, String authType) { } @Override public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; // Install the all-trusting trust manager SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); url = new URL(https_url); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); con.setRequestProperty("Authorization", Auth); BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); String input; while ((input = br.readLine()) != null) { ret += input; } br.close(); System.out.println(ret); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ConnectException e) { Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, "Exgotou o tempo de resposta"); System.out.println(""); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex); } catch (KeyManagementException ex) { Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException e) { e.printStackTrace(); } return ret; }
From source file:tk.egsf.ddns.JSON_helper.java
public static String postToUrl(String URL, String Auth, JSONObject post) { String ret = ""; String https_url = URL; URL url;//from w ww. j av a2 s . c o m try { TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted(X509Certificate[] certs, String authType) { } @Override public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; // Install the all-trusting trust manager SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); url = new URL(https_url); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); con.setRequestProperty("Authorization", Auth); con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Accept", "application/json"); con.setRequestMethod("POST"); con.setDoOutput(true); con.connect(); // Send post System.out.println(post.toString()); byte[] outputBytes = post.toString().getBytes("UTF-8"); OutputStream os = con.getOutputStream(); os.write(outputBytes); os.close(); // get response BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); String input; while ((input = br.readLine()) != null) { ret += input; } br.close(); System.out.println(ret); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { // e.printStackTrace(); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex); } catch (KeyManagementException ex) { Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex); } return ret; }
From source file:com.camel.trainreserve.JDKHttpsClient.java
public static ByteArrayOutputStream doGetImg(String url, String cookieStr) { InputStream in = null;/*from w w w . j a v a 2 s . c o m*/ ByteArrayOutputStream outStream = null; try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, new TrustManager[] { new DefaultTrustManager() }, new SecureRandom()); URL console = new URL(url); HttpsURLConnection conn = (HttpsURLConnection) console.openConnection(); conn.setRequestProperty("Cookie", cookieStr); conn.setSSLSocketFactory(sc.getSocketFactory()); conn.setHostnameVerifier(new TrustAnyHostnameVerifier()); conn.connect(); in = conn.getInputStream(); outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) != -1) { outStream.write(buffer, 0, len); } conn.disconnect(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (KeyManagementException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { in.close(); } catch (Exception e) { } } return outStream; }
From source file:com.clearcenter.mobile_demo.mdRest.java
static public String GetSystemInfo(String host, String token, long last_sample) throws JSONException, ParseException, IOException, AuthenticationException { try {/*from ww w. ja v a 2 s . c o m*/ URL url = new URL("https://" + host + URL_SYSINFO + "/" + last_sample); Log.v(TAG, "GetSystemInfo: host: " + host + ", token: " + token + ", URL: " + url); HttpsURLConnection http = CreateConnection(url); http.setRequestMethod("GET"); http.setRequestProperty("Cookie", token); final StringBuffer response = ProcessRequest(http); // Process response JSONObject json_data = null; try { //Log.i(TAG, "response: " + response.toString()); json_data = new JSONObject(response.toString()); } catch (JSONException e) { Log.e(TAG, "JSONException", e); return ""; } if (json_data.has("result")) { Integer result = RESULT_UNKNOWN; try { result = Integer.valueOf(json_data.getString("result")); } catch (NumberFormatException e) { } Log.d(TAG, "result: " + result.toString()); if (result == RESULT_SUCCESS && json_data.has("data")) { //Log.i(TAG, "data: " + json_data.getString("data")); return json_data.getString("data"); } if (result == RESULT_ACCESS_DENIED) throw new AuthenticationException(); // New cookies? final String cookie = http.getHeaderField("Set-Cookie"); if (cookie != null) { Log.d(TAG, "New cookie!"); } // All other results are failures... throw new IOException(); } } catch (MalformedURLException e) { Log.e(TAG, "MalformedURLException", e); throw new ParseException(); } Log.i(TAG, "Malformed result"); throw new IOException(); }
From source file:com.ct855.util.HttpsClientUtil.java
public static String testIt(String https_url, Map<String, String> map, String method) throws NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException { //SSLContext?? TrustManager[] trustAllCerts = new TrustManager[] { new MyX509TrustManager() }; SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); //SSLContextSSLSocketFactory SSLSocketFactory ssf = sslContext.getSocketFactory(); URL url;/*www . j a v a 2 s.c om*/ try { url = new URL(https_url); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); con.setRequestMethod(method); for (Map.Entry<String, String> entry : map.entrySet()) { con.setRequestProperty(entry.getKey(), entry.getValue()); } con.setSSLSocketFactory(ssf); //dumpl all cert info //print_https_cert(con); //dump all the content return print_content(con); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String executeHttpsPost(String url, String data, InputStream key) { HttpsURLConnection localHttpsURLConnection = null; try {//from w ww. ja v a 2 s . co m URL localURL = new URL(url); localHttpsURLConnection = (HttpsURLConnection) localURL.openConnection(); localHttpsURLConnection.setRequestMethod("POST"); localHttpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); localHttpsURLConnection.setRequestProperty("Content-Length", "" + Integer.toString(data.getBytes().length)); localHttpsURLConnection.setRequestProperty("Content-Language", "en-US"); localHttpsURLConnection.setUseCaches(false); localHttpsURLConnection.setDoInput(true); localHttpsURLConnection.setDoOutput(true); localHttpsURLConnection.connect(); Certificate[] arrayOfCertificate = localHttpsURLConnection.getServerCertificates(); byte[] arrayOfByte1 = new byte[294]; DataInputStream localDataInputStream = new DataInputStream(key); localDataInputStream.readFully(arrayOfByte1); localDataInputStream.close(); Certificate localCertificate = arrayOfCertificate[0]; PublicKey localPublicKey = localCertificate.getPublicKey(); byte[] arrayOfByte2 = localPublicKey.getEncoded(); for (int i = 0; i < arrayOfByte2.length; i++) { if (arrayOfByte2[i] != arrayOfByte1[i]) throw new RuntimeException("Public key mismatch"); } DataOutputStream localDataOutputStream = new DataOutputStream( localHttpsURLConnection.getOutputStream()); localDataOutputStream.writeBytes(data); localDataOutputStream.flush(); localDataOutputStream.close(); InputStream localInputStream = localHttpsURLConnection.getInputStream(); BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream)); StringBuffer localStringBuffer = new StringBuffer(); String str1; while ((str1 = localBufferedReader.readLine()) != null) { localStringBuffer.append(str1); localStringBuffer.append('\r'); } localBufferedReader.close(); return localStringBuffer.toString(); } catch (Exception localException) { byte[] arrayOfByte1; localException.printStackTrace(); return null; } finally { if (localHttpsURLConnection != null) localHttpsURLConnection.disconnect(); } }
From source file:org.schabi.newpipe.Downloader.java
/**Common functionality between download(String url) and download(String url, String language)*/ private static String dl(HttpsURLConnection con) throws IOException { StringBuilder response = new StringBuilder(); try {// w w w. ja v a 2 s. c o m con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", USER_AGENT); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); } catch (UnknownHostException uhe) {//thrown when there's no internet connection uhe.printStackTrace(); //Toast.makeText(getActivity(), uhe.getMessage(), Toast.LENGTH_LONG).show(); } return response.toString(); }