private static HttpsURLConnection getFeed(String downloadUrl) { HttpsURLConnection urlConnection = null; try { URL url = new URL(downloadUrl); urlConnection = (HttpsURLConnection) url.openConnection(); urlConnection.setUseCaches(false); urlConnection.setDefaultUseCaches(false); urlConnection.setRequestMethod("GET"); urlConnection.setRequestProperty( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36"); urlConnection.setRequestProperty("Upgrade-Insecure-Requests", "1"); urlConnection.setRequestProperty("Connection", "keep-alive"); urlConnection.setRequestProperty("Cache-Control", "max-age=0"); urlConnection.setRequestProperty( "Accept-Language", "en-US,en;q=0.8,fr-FR;q=0.6,fr;q=0.4,en-AU;q=0.2"); urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch"); urlConnection.setRequestProperty( "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); long currentTime = System.currentTimeMillis(); long expires = urlConnection.getHeaderFieldDate("Expires", currentTime); long lastModified = urlConnection.getHeaderFieldDate("Last-Modified", currentTime); urlConnection.setInstanceFollowRedirects(true); // InputStream inputStream = urlConnection.getInputStream(); return urlConnection; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // urlConnection.disconnect(); } return null; }
public static boolean isAddressReachable(String url) { URLConnection urlConnection = null; try { urlConnection = new URL(url).openConnection(); if (url.contains("https")) { HttpsURLConnection urlConnect = (HttpsURLConnection) urlConnection; urlConnect.setConnectTimeout(5000); urlConnect.setReadTimeout(30000); urlConnect.setInstanceFollowRedirects(false); urlConnect.setRequestMethod("HEAD"); int responseCode = urlConnect.getResponseCode(); urlConnect.disconnect(); urlConnect = null; return (responseCode == HttpURLConnection.HTTP_OK); } else { HttpURLConnection urlConnect = (HttpURLConnection) urlConnection; urlConnect.setConnectTimeout(5000); urlConnect.setReadTimeout(30000); urlConnect.setInstanceFollowRedirects(false); urlConnect.setRequestMethod("HEAD"); int responseCode = urlConnect.getResponseCode(); urlConnect.disconnect(); urlConnect = null; return (responseCode == HttpURLConnection.HTTP_OK); } } catch (Exception e) { } finally { if (urlConnection != null) { urlConnection = null; } } return false; }
public HttpsURLConnection openConnection() throws IOException { HttpsURLConnection connection = null; try { if (!isPost) { // 如果是get请求则拼接URL地址,一般情况下不会走https的get请求 mUrl += packageTextParamsForGet(); } // 初始化连接 URL connecter = new URL(mUrl); connection = (HttpsURLConnection) connecter.openConnection(); // 设置安全套接工厂 connection.setSSLSocketFactory(mSslContext.getSocketFactory()); connection.setConnectTimeout(TIME_OUT); connection.setReadTimeout(TIME_OUT); // connection.setRequestProperty("User-Agent", // "Mozilla/5.0 ( compatible ) "); connection.setRequestProperty("Accept", "*/*"); connection.setRequestProperty("Content-Type", "*/*;charset=UTF-8"); connection.setRequestProperty("Connection", "Keep-Alive"); if (isPost) { connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(false); } // 设置输入输出流 connection.setDoInput(true); if (isPost) connection.setDoOutput(true); } catch (IOException ioe) { throw ioe; } // 查询params里面是否有数据 DataOutputStream out = new DataOutputStream(connection.getOutputStream()); StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : mParams.entrySet()) sb.append(entry.getValue() + LINE_END); // 向输入流里面写入,需要提交的数据 out.write(sb.toString().getBytes()); out.flush(); out.close(); return connection; }
public String doPost(String urlAddress, Map<String, String> param) throws WeiboException { BeeboApplication globalContext = BeeboApplication.getInstance(); String errorStr = globalContext.getString(R.string.timeout); globalContext = null; try { URL url = new URL(urlAddress); Proxy proxy = getProxy(); HttpsURLConnection uRLConnection; if (proxy != null) { uRLConnection = (HttpsURLConnection) url.openConnection(proxy); } else { uRLConnection = (HttpsURLConnection) url.openConnection(); } uRLConnection.setDoInput(true); uRLConnection.setDoOutput(true); uRLConnection.setRequestMethod("POST"); uRLConnection.setUseCaches(false); uRLConnection.setConnectTimeout(CONNECT_TIMEOUT); uRLConnection.setReadTimeout(READ_TIMEOUT); uRLConnection.setInstanceFollowRedirects(false); uRLConnection.setRequestProperty("Connection", "Keep-Alive"); uRLConnection.setRequestProperty("Charset", "UTF-8"); uRLConnection.setRequestProperty("Accept-Encoding", "gzip, deflate"); uRLConnection.connect(); DataOutputStream out = new DataOutputStream(uRLConnection.getOutputStream()); out.write(Utility.encodeUrl(param).getBytes()); out.flush(); out.close(); return handleResponse(uRLConnection); } catch (IOException e) { e.printStackTrace(); throw new WeiboException(errorStr, e); } }
/** * Realiza una peticción https GET utilizando el proxy especificado (con certificado) * * @param sUrl Dirección http a resolver * @param sProxy Nombre del proxy, p.e. proxy.bde.es * @param sProxyPort Puerto del proxy, p.e. 80 * @return String con el texto devuelto * @throws MalformedURLException * @throws IOException */ public static String httpsGetCertificado( String sUrl, String sProxy, String sProxyPort, int timeoutMs, String alias, String keyStore, String trustStore, String keyStorePassword, String trustStorePassword, String pCookies, int numeroDeRedireccionActual) throws MalformedURLException, IOException { try { SSLSocketFactory sc = new SSLSocketFactoryGenerator( alias, keyStore, trustStore, keyStorePassword, trustStorePassword) .getSSLSocketFactory(); HttpsURLConnection.setDefaultSSLSocketFactory(sc); } catch (Exception e) { return null; } HttpsURLConnection con = null; URL url = new URL(sUrl); // Creamos la conexión con la url if (sProxy != null && sProxyPort != null) { Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(sProxy, Integer.parseInt(sProxyPort))); con = (HttpsURLConnection) url.openConnection(proxy); } else { con = (HttpsURLConnection) url.openConnection(); } // Evitamos que se sigan los redirects ya que los redirects los hace sin // pasar las cookies lo cual es un bug. con.setInstanceFollowRedirects(false); // Evitamos el uso de respuestas cacheadas con.setUseCaches(false); // Fijamos los timeouts if (timeoutMs > 0) { con.setConnectTimeout(timeoutMs); con.setReadTimeout(timeoutMs); } // Respuesta de la petición con.connect(); InputStream in = con.getInputStream(); StringBuffer sb = new StringBuffer(); int c; while ((c = in.read()) != -1) sb.append((char) c); int responseCode = con.getResponseCode(); String cookies = HttpUtil.getResponseHeader((URLConnection) con, "Set-Cookie"); if (!StringUtils.isEmpty(pCookies) && !StringUtils.isEmpty(cookies)) { cookies = pCookies + "; " + cookies; } String location = HttpUtil.getResponseHeaderLocation(sUrl, (URLConnection) con); con.disconnect(); // Comprobación de si es una redirección if (responseCode == 301 || responseCode == 302) { if (numeroDeRedireccionActual < 10) { return httpsGetCertificado( location, sProxy, sProxyPort, timeoutMs, alias, keyStore, trustStore, keyStorePassword, trustStorePassword, cookies, numeroDeRedireccionActual + 1); } else { throw new IOException( "Se han producido 10 redirecciones consecutivas al solicitar la página web. Se cancela la petición"); } } else { return sb.toString(); } }
/** * Realiza una peticción https GET utilizando el proxy especificado * * @param sUrl Dirección http a resolver * @param sProxy Nombre del proxy, p.e. proxy.bde.es * @param sProxyPort Puerto del proxy, p.e. 80 * @return String con el texto devuelto * @throws MalformedURLException * @throws IOException */ public static String httpsGet( String sUrl, String sProxy, String sProxyPort, int timeoutMs, String pCookies, int numeroDeRedireccionActual) throws MalformedURLException, IOException { // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) {} @Override public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) {} } }; // Install the all-trusting trust manager try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { return null; } HttpsURLConnection con = null; URL url = new URL(sUrl); // Creamos la conexión con la url if (sProxy != null && sProxyPort != null) { Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(sProxy, Integer.parseInt(sProxyPort))); con = (HttpsURLConnection) url.openConnection(proxy); } else { con = (HttpsURLConnection) url.openConnection(); } // Evitamos que se sigan los redirects ya que los redirects los hace sin // pasar las cookies lo cual es un bug. con.setInstanceFollowRedirects(false); // Evitamos el uso de respuestas cacheadas con.setUseCaches(false); // Fijamos los timeouts if (timeoutMs > 0) { con.setConnectTimeout(timeoutMs); con.setReadTimeout(timeoutMs); } // Enviamos las cookies if (!StringUtils.isEmpty(pCookies)) { con.setRequestProperty("Cookie", pCookies); } // Respuesta de la petición con.connect(); InputStream in = con.getInputStream(); StringBuffer sb = new StringBuffer(); int c; while ((c = in.read()) != -1) sb.append((char) c); int responseCode = con.getResponseCode(); String cookies = HttpUtil.getResponseHeader((URLConnection) con, "Set-Cookie"); if (!StringUtils.isEmpty(pCookies) && !StringUtils.isEmpty(cookies)) { cookies = pCookies + "; " + cookies; } String location = HttpUtil.getResponseHeaderLocation(sUrl, (URLConnection) con); con.disconnect(); // Comprobación de si es una redirección if (responseCode == 301 || responseCode == 302) { if (numeroDeRedireccionActual < 10) { Logger.getRootLogger().debug("Redirigiendo a " + location); Logger.getRootLogger().debug("Cookies enviadas:" + cookies); return httpsGet( location, sProxy, sProxyPort, timeoutMs, cookies, numeroDeRedireccionActual); } else { throw new IOException( "Se han producido 10 redirecciones consecutivas al solicitar la página web. Se cancela la petición"); } } else { return sb.toString(); } }
public void recursiveRequest(String path, String reffer) { URL url = null; try { url = new URL(path); conn = (HttpsURLConnection) url.openConnection(); // 同步接口获取IP String ip = httpdns.getIpByHostAsync(url.getHost()); if (ip != null) { // 通过HTTPDNS获取IP成功,进行URL替换和HOST头设置 Log.d(TAG, "Get IP: " + ip + " for host: " + url.getHost() + " from HTTPDNS successfully!"); String newUrl = path.replaceFirst(url.getHost(), ip); conn = (HttpsURLConnection) new URL(newUrl).openConnection(); // 设置HTTP请求头Host域 conn.setRequestProperty("Host", url.getHost()); } conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setInstanceFollowRedirects(false); TlsSniSocketFactory sslSocketFactory = new TlsSniSocketFactory(conn); conn.setSSLSocketFactory(sslSocketFactory); conn.setHostnameVerifier( new HostnameVerifier() { /* * 关于这个接口的说明,官方有文档描述: * This is an extended verification option that implementers can provide. * It is to be used during a handshake if the URL's hostname does not match the * peer's identification hostname. * * 使用HTTPDNS后URL里设置的hostname不是远程的主机名(如:m.taobao.com),与证书颁发的域不匹配, * Android HttpsURLConnection提供了回调接口让用户来处理这种定制化场景。 * 在确认HTTPDNS返回的源站IP与Session携带的IP信息一致后,您可以在回调方法中将待验证域名替换为原来的真实域名进行验证。 * */ @Override public boolean verify(String hostname, SSLSession session) { String host = conn.getRequestProperty("Host"); if (null == host) { host = conn.getURL().getHost(); } return HttpsURLConnection.getDefaultHostnameVerifier().verify(host, session); } }); int code = conn.getResponseCode(); // Network block if (needRedirect(code)) { // 临时重定向和永久重定向location的大小写有区分 String location = conn.getHeaderField("Location"); if (location == null) { location = conn.getHeaderField("location"); } if (!(location.startsWith("http://") || location.startsWith("https://"))) { // 某些时候会省略host,只返回后面的path,所以需要补全url URL originalUrl = new URL(path); location = originalUrl.getProtocol() + "://" + originalUrl.getHost() + location; } recursiveRequest(location, path); } else { // redirect finish. DataInputStream dis = new DataInputStream(conn.getInputStream()); int len; byte[] buff = new byte[4096]; StringBuilder response = new StringBuilder(); while ((len = dis.read(buff)) != -1) { response.append(new String(buff, 0, len)); } Log.d(TAG, "Response: " + response.toString()); } } catch (MalformedURLException e) { Log.w(TAG, "recursiveRequest MalformedURLException"); } catch (IOException e) { Log.w(TAG, "recursiveRequest IOException"); } catch (Exception e) { Log.w(TAG, "unknow exception"); } finally { if (conn != null) { conn.disconnect(); } } }