List of usage examples for java.net HttpURLConnection connect
public abstract void connect() throws IOException;
From source file:commonUtils.CommonUtils.java
public static boolean IsExistsLink(String inputLink) { try {/*from w w w. ja v a 2 s. c om*/ URL url = new URL(inputLink); HttpURLConnection huc = (HttpURLConnection) url.openConnection(); huc.setRequestMethod("GET"); huc.connect(); System.out.println(huc.getResponseCode()); if (huc.getResponseCode() == 200) { return true; } } catch (Exception ex) { return false; } return false; }
From source file:com.adguard.compiler.UrlUtils.java
public static String downloadString(URL url, String encoding) throws IOException { HttpURLConnection connection = null; InputStream inputStream = null; try {/*from w w w. j a va 2 s . co m*/ connection = (HttpURLConnection) url.openConnection(); connection.connect(); inputStream = connection.getInputStream(); return IOUtils.toString(inputStream, encoding); } finally { IOUtils.closeQuietly(inputStream); if (connection != null) { connection.disconnect(); } } }
From source file:Main.java
public static HttpURLConnection openUrlConnection(String url) throws IOException { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setUseCaches(false);/* w ww . ja va 2s . co m*/ conn.setChunkedStreamingMode(0); conn.setRequestProperty("User-Agent", USER_AGENT); conn.connect(); return conn; }
From source file:Main.java
private static String httpGet(String address) { InputStream inputStream = null; HttpURLConnection httpURLConnection = null; try {/*from w ww .j a v a 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 InputStream downloadURL(String link) throws IOException { URL url = new URL(link); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000);/* www .ja v a 2s.com*/ conn.setConnectTimeout(15000); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.connect(); logInfo("downloadStatus: " + conn.getResponseCode()); return conn.getInputStream(); }
From source file:Main.java
static public String downloadFile(String downloadUrl, String fileName, String dir) throws IOException { URL url = new URL(downloadUrl); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true);//from w w w . ja v a 2s . c o m urlConnection.connect(); int code = urlConnection.getResponseCode(); if (code > 300 || code == -1) { throw new IOException("Cannot read url: " + downloadUrl); } String filePath = prepareFilePath(fileName, dir); String tmpFilePath = prepareTmpFilePath(fileName, dir); FileOutputStream fileOutput = new FileOutputStream(tmpFilePath); BufferedInputStream inputStream = new BufferedInputStream(urlConnection.getInputStream()); byte[] buffer = new byte[1024]; int bufferLength; while ((bufferLength = inputStream.read(buffer)) > 0) { fileOutput.write(buffer, 0, bufferLength); } fileOutput.close(); // move tmp to destination copyFile(new File(tmpFilePath), new File(filePath)); return filePath; }
From source file:Main.java
public static String request(String httpUrl, String httpArg) { BufferedReader reader = null; String result = null;/*from ww w. j av a 2 s . c o m*/ StringBuffer sbf = new StringBuffer(); httpUrl = httpUrl + "?" + httpArg; try { URL url = new URL(httpUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("apikey", "2ffbcae4d025b6c109af30d7de2d7c09"); connection.connect(); InputStream is = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String strRead = null; while ((strRead = reader.readLine()) != null) { sbf.append(strRead); sbf.append("\r\n"); } reader.close(); result = sbf.toString(); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static Reader getUri(URL url) throws IOException { //Log.d(TAG, "getUri: " + url.toString()); boolean useGzip = false; HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(30 * 1000);/*from ww w . j a va 2s . c o m*/ conn.setRequestProperty("Accept-Encoding", "gzip"); conn.connect(); InputStream in = conn.getInputStream(); final Map<String, List<String>> headers = conn.getHeaderFields(); // This is a map, but we can't assume the key we're looking for // is in normal casing. So it's really not a good map, is it? final Set<Map.Entry<String, List<String>>> set = headers.entrySet(); for (Iterator<Map.Entry<String, List<String>>> i = set.iterator(); i.hasNext();) { Map.Entry<String, List<String>> entry = i.next(); if ("Content-Encoding".equalsIgnoreCase(entry.getKey())) { for (Iterator<String> j = entry.getValue().iterator(); j.hasNext();) { String str = j.next(); if (str.equalsIgnoreCase("gzip")) { useGzip = true; break; } } // Break out of outer loop. if (useGzip) { break; } } } if (useGzip) { return new BufferedReader(new InputStreamReader(new GZIPInputStream(in)), 8 * 1024); } else { return new BufferedReader(new InputStreamReader(in), 8 * 1024); } }
From source file:net.idlesoft.android.apps.github.utils.GravatarCache.java
private static Bitmap downloadGravatar(final String id) throws IOException { final URL aURL = new URL("http://www.gravatar.com/avatar/" + URLEncoder.encode(id) + "?size=100&d=mm"); final HttpURLConnection conn = (HttpURLConnection) aURL.openConnection(); conn.setDoInput(true);/*from ww w.j a va2 s. c o m*/ conn.connect(); final InputStream is = conn.getInputStream(); final Bitmap bm = BitmapFactory.decodeStream(is); is.close(); return bm; }
From source file:edu.rit.chrisbitler.ritcraft.slackintegration.rtm.UserList.java
/** * Query the slack api to get the list of users and their real names. This is done whenever we can't find an ID mapping, * and when the plugin loads/*from www. j a v a 2s.co m*/ */ public static void queryUsers() { try { users.clear(); //Contact the slack api to get the user list HttpURLConnection conn = (HttpURLConnection) new URL( "https://www.slack.com/api/users.list?token=" + SlackIntegration.BOT_TOKEN).openConnection(); conn.connect(); JSONObject retVal = (JSONObject) JSONValue .parse(new InputStreamReader((InputStream) conn.getContent())); JSONArray members = (JSONArray) retVal.get("members"); //Loop through the members and add them to the map Iterator<JSONObject> iter = members.iterator(); while (iter.hasNext()) { JSONObject obj = iter.next(); JSONObject profile = (JSONObject) obj.get("profile"); String id = (String) obj.get("id"); String realName = (String) profile.get("real_name"); users.put(id, realName); } } catch (IOException e) { e.printStackTrace(); } }