List of usage examples for java.net HttpURLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:cc.vileda.sipgatesync.api.SipgateApi.java
@NonNull private static HttpURLConnection getConnection(String apiUrl) throws IOException { final URL url = new URL(HTTPS_API_SIPGATE_COM_V1 + apiUrl); final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestProperty("Content-Type", "application/json"); urlConnection.setRequestProperty("Accept", "application/json"); return urlConnection; }
From source file:Main.java
public static boolean hasActiveInternetConnection(Context context) { if (isNetworkAvailable(context)) { try {//from w w w .ja va 2 s.c om HttpURLConnection connection = (HttpURLConnection) (new URL( "http://clients3.google.com/generate_204")).openConnection(); connection.setRequestProperty("User-Agent", "Test"); connection.setRequestProperty("Connection", "close"); connection.setReadTimeout(1500); connection.connect(); return (connection.getResponseCode() == 204 && connection.getContentLength() == 0); } catch (IOException e) { Log.e("ERROR", "Error checking internet connection"); } } else Log.e("ERROR", "No network available"); return false; }
From source file:Main.java
private static InputStream getInputStreamFromUrl_V9(Context paramContext, String paramString) { if (confirmDownload(paramContext, paramString)) { try {// www .j ava2 s.c o m URL localURL = new URL(paramString); HttpURLConnection localHttpURLConnection2 = (HttpURLConnection) localURL.openConnection(); localHttpURLConnection2.setRequestProperty("Accept-Charset", "UTF-8"); localHttpURLConnection2.setReadTimeout(30000); localHttpURLConnection2.setConnectTimeout(30000); localHttpURLConnection2.setRequestMethod("GET"); localHttpURLConnection2.setDoInput(true); localHttpURLConnection2.connect(); return localHttpURLConnection2.getInputStream(); } catch (Throwable localThrowable) { localThrowable.printStackTrace(); return null; } } else { return null; } }
From source file:Main.java
public static String get(String url) { try {/*from ww w. j ava 2s.c om*/ URL u = new URL(url); HttpURLConnection connection = (HttpURLConnection) u.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Charset", "UTF-8"); InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String response = ""; String readLine = ""; while ((readLine = br.readLine()) != null) { response += readLine; } br.close(); return response; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:com.blogspot.ryanfx.auth.GoogleUtil.java
/** * Sends the auth token to google and gets the json result. * @param token auth token/*from w w w.j av a 2s. co m*/ * @return json result if valid request, null if invalid. * @throws IOException * @throws LoginException */ private static String issueTokenGetRequest(String token) throws IOException, LoginException { int timeout = 2000; URL u = new URL("https://www.googleapis.com/oauth2/v2/userinfo"); HttpURLConnection c = (HttpURLConnection) u.openConnection(); c.setRequestMethod("GET"); c.setRequestProperty("Content-length", "0"); c.setRequestProperty("Authorization", "OAuth " + token); c.setUseCaches(false); c.setAllowUserInteraction(false); c.setConnectTimeout(timeout); c.setReadTimeout(timeout); c.connect(); int status = c.getResponseCode(); if (status == HttpServletResponse.SC_OK) { BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); return sb.toString(); } else if (status == HttpServletResponse.SC_UNAUTHORIZED) { Logger.getLogger(GoogleUtil.class.getName()).severe("Invalid token request: " + token); } return null; }
From source file:com.jlgranda.fede.ejb.url.reader.FacturaElectronicaURLReader.java
/** * Leer contenido texto UTF-8 desde el URL dado * * @param _url el URL a leer/*from www . j av a 2 s . c o m*/ * @return el contenido del URL como String * @throws Exception */ public static String read(String _url) throws Exception { StringBuilder b = new StringBuilder(); try { //logger.info("HTTP request: " + _url); final URL url = new URL(_url); final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31"); b.append(slurp(connection.getInputStream(), 1024)); connection.disconnect(); } catch (IOException e) { e.printStackTrace(); } return b.toString(); }
From source file:com.microsoft.webapp.util.WebAppUtils.java
public static void sendGet(String sitePath) throws Exception { URL url = new URL(sitePath); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", "AzureToolkit for Eclipse"); con.getResponseCode();// w ww. jav a 2 s .c om }
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 w ww . j a v a 2 s. 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:Main.java
public static String request(String url, Map<String, String> cookies, Map<String, String> parameters) throws Exception { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"); if (cookies != null && !cookies.isEmpty()) { StringBuilder cookieHeader = new StringBuilder(); for (String cookie : cookies.values()) { if (cookieHeader.length() > 0) { cookieHeader.append(";"); }/*from w w w. j a v a2 s . c o m*/ cookieHeader.append(cookie); } connection.setRequestProperty("Cookie", cookieHeader.toString()); } connection.setDoInput(true); if (parameters != null && !parameters.isEmpty()) { connection.setDoOutput(true); OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream()); osw.write(parametersToWWWFormURLEncoded(parameters)); osw.flush(); osw.close(); } if (cookies != null) { for (Map.Entry<String, List<String>> headerEntry : connection.getHeaderFields().entrySet()) { if (headerEntry != null && headerEntry.getKey() != null && headerEntry.getKey().equalsIgnoreCase("Set-Cookie")) { for (String header : headerEntry.getValue()) { for (HttpCookie httpCookie : HttpCookie.parse(header)) { cookies.put(httpCookie.getName(), httpCookie.toString()); } } } } } Reader r = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringWriter w = new StringWriter(); char[] buffer = new char[1024]; int n = 0; while ((n = r.read(buffer)) != -1) { w.write(buffer, 0, n); } r.close(); return w.toString(); }
From source file:com.quackware.handsfreemusic.Utility.java
public static String getSourceCode(URL url) { Object content = null;//w w w .j a va 2 s . c o m try { HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.4) Gecko/20100513 Firefox/3.6.4"); uc.connect(); InputStream stream = uc.getInputStream(); if (stream != null) { content = readStream(uc.getContentLength(), stream); } else if ((content = uc.getContent()) != null && content instanceof java.io.InputStream) content = readStream(uc.getContentLength(), (java.io.InputStream) content); uc.disconnect(); } catch (Exception ex) { return null; } if (content != null && content instanceof String) { String html = (String) content; return html; } else { return null; } }