List of usage examples for java.net HttpURLConnection HTTP_OK
int HTTP_OK
To view the source code for java.net HttpURLConnection HTTP_OK.
Click Source Link
From source file:Main.java
/** * Get image from newwork /* ww w . jav a 2 s .co m*/ * @param path The path of image * @return InputStream * @throws Exception */ public static InputStream getImageStream(String path) throws Exception { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5 * 1000); conn.setRequestMethod("GET"); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { return conn.getInputStream(); } return null; }
From source file:Main.java
private static String httpGet(String address) { InputStream inputStream = null; HttpURLConnection httpURLConnection = null; try {/*from w ww. j a va 2s .c om*/ URL url = new URL(address); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.connect(); if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { inputStream = httpURLConnection.getInputStream(); int len = 0; byte[] buffer = new byte[1024]; StringBuffer stringBuffer = new StringBuffer(); while ((len = inputStream.read(buffer)) != -1) { stringBuffer.append(new String(buffer, 0, len)); } return stringBuffer.toString(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { close(inputStream); if (httpURLConnection != null) { httpURLConnection.disconnect(); } } return null; }
From source file:Main.java
public static String getJsonWithPath(String path) { StringBuffer sb = new StringBuffer(); URL url = null;/* w w w . jav a2s . c o m*/ HttpURLConnection conn = null; BufferedReader br = null; try { url = new URL(path); conn = (HttpURLConnection) url.openConnection(); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { br = new BufferedReader(new InputStreamReader(conn.getInputStream())); String temp = ""; while ((temp = br.readLine()) != null) { sb.append(temp); } } else { Log.e(TAG, " NET IS ERROR"); } } catch (Exception e) { e.printStackTrace(); } finally { close(br); conn.disconnect(); } Log.i("TAG", "---" + sb.toString()); return sb.toString(); }
From source file:Main.java
private static InputStream OpenHttpConnection(String strURL, Boolean cache) throws IOException { InputStream inputStream = null; try {//from w w w. ja va 2s.com URL url = new URL(strURL); URLConnection conn = url.openConnection(); conn.setUseCaches(cache); HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setRequestMethod("GET"); httpConn.connect(); if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) inputStream = httpConn.getInputStream(); } catch (Exception ex) { } return inputStream; }
From source file:Main.java
static public String download_json(String url_addr) { StringBuilder result = new StringBuilder(); try {//from ww w .j av a 2 s. com URL url = new URL(url_addr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); if (conn != null) { conn.setConnectTimeout(10000); conn.setUseCaches(false); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); for (;;) { String line = br.readLine(); if (line == null) break; result.append(line + '\n'); } br.close(); } else { conn.disconnect(); return null; } conn.disconnect(); } } catch (Exception e) { Log.e(TAG, e.toString()); return null; } return result.toString(); }
From source file:Main.java
private static InputStream getHttpConnection(String urlString) throws IOException { InputStream stream = null;//w ww .j a v a2 s . co m URL url = new URL(urlString); URLConnection connection = url.openConnection(); try { HttpURLConnection httpConnection = (HttpURLConnection) connection; httpConnection.setRequestMethod("GET"); httpConnection.connect(); if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { stream = httpConnection.getInputStream(); } } catch (Exception ex) { ex.printStackTrace(); } return stream; }
From source file:Main.java
public static byte[] downloadImage(final URL url) { InputStream in = null;//from www. j av a2 s.c o m int responseCode = -1; try { final HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoInput(true); con.connect(); responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { in = con.getInputStream(); return getByteFromInputStream(in); } } catch (final IOException e) { Log.e(LOG_TAG, "Failed to download image from: " + url.toString(), e); } return null; }
From source file:Main.java
public static byte[] getHtmlByteArray(final String url) { URL htmlUrl;//from w w w.j ava 2 s . c o m InputStream inStream = null; try { htmlUrl = new URL(url); URLConnection connection = htmlUrl.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection) connection; int responseCode = httpConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { inStream = httpConnection.getInputStream(); } } catch (IOException e) { e.printStackTrace(); } byte[] data = inputStreamToByte(inStream); return data; }
From source file:Main.java
/** * Makes HttpURLConnection and returns InputStream *//*ww w .j a v a2s .co m*/ private static InputStream getHttpConnection(String urlString) throws IOException { InputStream stream = null; URL url = new URL(urlString); URLConnection connection = url.openConnection(); try { HttpURLConnection httpConnection = (HttpURLConnection) connection; httpConnection.setRequestMethod("GET"); httpConnection.connect(); if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { stream = httpConnection.getInputStream(); } } catch (Exception ex) { ex.printStackTrace(); } return stream; }
From source file:Main.java
@SuppressWarnings("unused") public static void selectServer2() { for (String host : host_arr) { HttpURLConnection conn = null; try {//from ww w . j a v a 2 s.c o m conn = (HttpURLConnection) new URL(host).openConnection(); conn.setConnectTimeout(6000); int result = conn.getResponseCode(); String re = conn.getResponseMessage(); if (result == HttpURLConnection.HTTP_OK) { HOST = host;// break; } else { } } catch (MalformedURLException e) { } catch (IOException e) { } finally { if (conn != null) { conn.disconnect(); conn = null; } } } }