List of usage examples for java.net HttpURLConnection setInstanceFollowRedirects
public void setInstanceFollowRedirects(boolean followRedirects)
From source file:org.osmdroid.location.FourSquareProvider.java
public static void browse(final Context context, POI poi) { // get the right url from redirect, could also parse the result from querying venueid... new AsyncTask<POI, Void, String>() { @Override/* w w w . j a v a 2s . c om*/ protected String doInBackground(POI... params) { POI poi = params[0]; if (poi == null) return null; try { URL url = new URL(poi.url); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setInstanceFollowRedirects(false); String redirect = conn.getHeaderField("Location"); if (redirect != null) { Log.d(BonusPackHelper.LOG_TAG, redirect); return redirect; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { if (result == null) return; Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://foursquare.com" + result)); context.startActivity(myIntent); } }.execute(poi); }
From source file:org.jvnet.hudson.update_center.ConfluencePluginList.java
private static HttpURLConnection connect(String url, String sessionId) throws IOException { HttpURLConnection huc = (HttpURLConnection) new URL(url).openConnection(); huc.setInstanceFollowRedirects(false); huc.setDoOutput(false);// www . jav a 2 s . co m if (sessionId != null) huc.addRequestProperty("Cookie", sessionId); InputStream i = huc.getInputStream(); while (i.read() >= 0) ; // Drain stream return huc; }
From source file:Main.java
/** * Given a string url, connects and returns response code * * @param urlString string to fetch * @param readTimeOutMs read time out * @param connectionTimeOutMs connection time out * @param urlRedirect should use urlRedirect * @param useCaches should use cache * @return httpResponseCode http response code * @throws IOException/*from ww w . j a v a 2 s .co m*/ */ public static int checkUrlWithOptions(String urlString, int readTimeOutMs, int connectionTimeOutMs, boolean urlRedirect, boolean useCaches) throws IOException { URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setReadTimeout(readTimeOutMs /* milliseconds */); connection.setConnectTimeout(connectionTimeOutMs /* milliseconds */); connection.setRequestMethod("GET"); connection.setInstanceFollowRedirects(urlRedirect); connection.setUseCaches(useCaches); // Starts the query connection.connect(); int responseCode = connection.getResponseCode(); connection.disconnect(); return responseCode; }
From source file:org.lightjason.agentspeak.action.buildin.rest.IBaseRest.java
/** * creates a HTTP connection and reads the data * * @param p_url url/*from w w w .jav a 2 s . c o m*/ * @return url data * @throws IOException is thrown on connection errors */ private static String httpdata(final String p_url) throws IOException { final HttpURLConnection l_connection = (HttpURLConnection) new URL(p_url).openConnection(); // follow HTTP redirects l_connection.setInstanceFollowRedirects(true); // set a HTTP-User-Agent if not exists l_connection.setRequestProperty("User-Agent", (System.getProperty("http.agent") == null) || (System.getProperty("http.agent").isEmpty()) ? CCommon.PACKAGEROOT + CCommon.configuration().getString("version") : System.getProperty("http.agent")); // read stream data final InputStream l_stream = l_connection.getInputStream(); final String l_return = CharStreams.toString(new InputStreamReader(l_stream, (l_connection.getContentEncoding() == null) || (l_connection.getContentEncoding().isEmpty()) ? Charsets.UTF_8 : Charset.forName(l_connection.getContentEncoding()))); Closeables.closeQuietly(l_stream); return l_return; }
From source file:org.lightjason.agentspeak.action.builtin.rest.IBaseRest.java
/** * creates a HTTP connection and reads the data * * @param p_url url/* w w w. j a va 2 s . com*/ * @return url data * * @throws IOException is thrown on connection errors */ @Nonnull private static String httpdata(@Nonnull final String p_url) throws IOException { final HttpURLConnection l_connection = (HttpURLConnection) new URL(p_url).openConnection(); // follow HTTP redirects l_connection.setInstanceFollowRedirects(true); // set a HTTP-User-Agent if not exists l_connection.setRequestProperty("User-Agent", (System.getProperty("http.agent") == null) || (System.getProperty("http.agent").isEmpty()) ? CCommon.PACKAGEROOT + CCommon.configuration().getString("version") : System.getProperty("http.agent")); // read stream data final InputStream l_stream = l_connection.getInputStream(); final String l_return = CharStreams.toString(new InputStreamReader(l_stream, (l_connection.getContentEncoding() == null) || (l_connection.getContentEncoding().isEmpty()) ? Charsets.UTF_8 : Charset.forName(l_connection.getContentEncoding()))); Closeables.closeQuietly(l_stream); return l_return; }
From source file:Main.java
/** * Given a string url, connects and returns response code * * @param urlString string to fetch * @param network network//from w w w. j av a 2 s . com * @param readTimeOutMs read time out * @param connectionTimeOutMs connection time out * @param urlRedirect should use urlRedirect * @param useCaches should use cache * @return httpResponseCode http response code * @throws IOException */ @TargetApi(LOLLIPOP) public static int checkUrlWithOptionsOverNetwork(String urlString, Network network, int readTimeOutMs, int connectionTimeOutMs, boolean urlRedirect, boolean useCaches) throws IOException { if (network == null) { return -1; } URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) network.openConnection(url); connection.setReadTimeout(readTimeOutMs /* milliseconds */); connection.setConnectTimeout(connectionTimeOutMs /* milliseconds */); connection.setRequestMethod("GET"); connection.setInstanceFollowRedirects(urlRedirect); connection.setUseCaches(useCaches); // Starts the query connection.connect(); int responseCode = connection.getResponseCode(); connection.disconnect(); return responseCode; }
From source file:jmc.util.UtlFbComents.java
public static String getJSONComentarios(String url, Long numComents) throws JMCException { String linea = ""; String buf = ""; try {// w w w . j av a 2 s.c o m Properties props = ConfigPropiedades.getProperties("props_config.properties"); CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); URL ur = new URL( "http://graph.facebook.com/comments?id=" + url + "&limit=" + numComents + "&filter=stream"); HttpURLConnection cn = (HttpURLConnection) ur.openConnection(); cn.setRequestProperty("user-agent", props.getProperty("navegador")); cn.setInstanceFollowRedirects(false); cn.setUseCaches(false); cn.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(cn.getInputStream())); while ((linea = br.readLine()) != null) { buf.concat(linea); } cn.disconnect(); } catch (IOException e) { throw new JMCException(e); } return buf; }
From source file:com.vinexs.tool.NetworkManager.java
public static void haveInternet(Context context, final OnInternetResponseListener listener) { if (!haveNetwork(context)) { listener.onResponsed(false);// w w w . ja va 2s.c o m return; } Log.d("Network", "Check internet is reachable ..."); new AsyncTask<Void, Integer, Boolean>() { @Override protected Boolean doInBackground(Void... params) { try { InetAddress ipAddr = InetAddress.getByName("google.com"); if (ipAddr.toString().equals("")) { throw new Exception("Cannot resolve host name, no internet behind network."); } HttpURLConnection conn = (HttpURLConnection) new URL("http://google.com/").openConnection(); conn.setInstanceFollowRedirects(false); conn.setConnectTimeout(3500); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); DataOutputStream postOutputStream = new DataOutputStream(conn.getOutputStream()); postOutputStream.write('\n'); postOutputStream.close(); conn.disconnect(); Log.d("Network", "Internet is reachable."); return true; } catch (Exception e) { e.printStackTrace(); } Log.d("Network", "Internet is unreachable."); return false; } @Override protected void onPostExecute(Boolean result) { listener.onResponsed(result); } }.execute(); }
From source file:qurandwnld.SyncDbHelper.java
public static ServerResponse sendMySelectionsToServer(String username, String password) throws Exception { String urlParameters = String.format("username=%s&password=%s&xmlData=%s", URLEncoder.encode(username, "UTF-8"), URLEncoder.encode(password, "UTF-8"), URLEncoder.encode(getSendXml(), "UTF-8")); byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); int postDataLength = postData.length; String request = "http://localhost:55622/Server/SubmitUserSelections"; URL url = new URL(request); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true);/*from w ww .j av a 2s . com*/ conn.setInstanceFollowRedirects(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("charset", "utf-8"); conn.setRequestProperty("Content-Length", Integer.toString(postDataLength)); conn.setUseCaches(false); try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) { wr.write(postData); } Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); StringBuilder json = new StringBuilder(); for (int c = in.read(); c != -1; c = in.read()) json.append((char) c); JSONObject obj = new JSONObject(json.toString()); ServerResponse res = new ServerResponse(); res.code = obj.getInt("Code"); res.message = obj.getString("Message"); return res; }
From source file:Main.java
static InputStream openRemoteInputStream(Uri uri) { URL finalUrl;/*from ww w . jav a 2 s . c o m*/ try { finalUrl = new URL(uri.toString()); } catch (MalformedURLException e) { e.printStackTrace(); return null; } HttpURLConnection connection; try { connection = (HttpURLConnection) finalUrl.openConnection(); } catch (IOException e) { e.printStackTrace(); return null; } connection.setInstanceFollowRedirects(false); int code; try { code = connection.getResponseCode(); } catch (IOException e) { e.printStackTrace(); return null; } if ((code == 301) || (code == 302) || (code == 303)) { String newLocation = connection.getHeaderField("Location"); return openRemoteInputStream(Uri.parse(newLocation)); } try { return (InputStream) finalUrl.getContent(); } catch (IOException e) { e.printStackTrace(); } return null; }