Java Code Examples for java.net.httpurlconnection#setFollowRedirects()

The following examples show how to use java.net.httpurlconnection#setFollowRedirects() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DynamoDBLocal.java    From geowave with Apache License 2.0 6 votes vote down vote up
protected boolean install() throws IOException {
  HttpURLConnection.setFollowRedirects(true);
  final URL url = new URL(DYNDB_URL + DYNDB_TAR);

  final File downloadFile = new File(dynLocalDir, DYNDB_TAR);
  if (!downloadFile.exists()) {
    try (FileOutputStream fos = new FileOutputStream(downloadFile)) {
      IOUtils.copyLarge(url.openStream(), fos);
      fos.flush();
    }
  }

  final TarGZipUnArchiver unarchiver = new TarGZipUnArchiver();
  unarchiver.enableLogging(new ConsoleLogger(Logger.WARN, "DynamoDB Local Unarchive"));
  unarchiver.setSourceFile(downloadFile);
  unarchiver.setDestDirectory(dynLocalDir);
  unarchiver.extract();

  if (!downloadFile.delete()) {
    LOGGER.warn("cannot delete " + downloadFile.getAbsolutePath());
  }

  // Check the install
  if (!isInstalled()) {
    LOGGER.error("DynamoDB Local install failed");
    return false;
  }

  return true;
}
 
Example 2
Source File: LazyLoadEmoji.java    From ChatGameFontificator with The Unlicense 6 votes vote down vote up
public static boolean checkUrl(URL url)
{
    try
    {
        HttpURLConnection.setFollowRedirects(true); // Does not work for http to https redirects
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("HEAD");
        httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
        int responseCode = httpURLConnection.getResponseCode();
        return responseCode == HttpURLConnection.HTTP_OK;
    }
    catch (Exception e)
    {
        logger.debug(e.getMessage(), e);
        return false;
    }
}
 
Example 3
Source File: OsioKeycloakTestAuthServiceClient.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
private KeycloakToken obtainActiveToken(String username, String password)
    throws IOException, MalformedURLException, ProtocolException, UnsupportedEncodingException {
  HttpURLConnection.setFollowRedirects(true);
  HttpURLConnection conn;
  String formPostURL = loginAndGetFormPostURL();

  HttpURLConnection.setFollowRedirects(false);

  conn = fillFormAndCreateConnection(formPostURL, username, password);

  String tokenJsonString = followRedirects(conn);

  // "token_json={}"
  String tokenJson =
      URLDecoder.decode(new URL(tokenJsonString).getQuery(), "UTF-8").substring(11);
  KeycloakToken readerToKeycloakToken = readerToKeycloakToken(new StringReader(tokenJson));
  return readerToKeycloakToken;
}
 
Example 4
Source File: PollServerAndRestart.java    From webdsl with Apache License 2.0 6 votes vote down vote up
public static boolean findContents(String url, String check){
    try {
        URL url1 = new URL(url);
        HttpURLConnection huc = (HttpURLConnection) url1.openConnection();
        HttpURLConnection.setFollowRedirects(false);
                huc.setReadTimeout(10 * 1000);
        huc.setConnectTimeout(10 * 1000);
        huc.setRequestMethod("GET");
        huc.connect();
        BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream()));
        String buffer = "";
        while (buffer != null) {
            if(buffer.contains(check)){
                return true;
            }
            buffer = br.readLine();
        }
        huc.disconnect();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return false;		
}
 
Example 5
Source File: Network.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public static InputStream downloadXmlAsStreamWithoutRedirect(URL url, String s, String s1)
{
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection httpurlconnection = (HttpURLConnection)url.openConnection();
    httpurlconnection.setConnectTimeout(10000);
    httpurlconnection.setReadTimeout(15000);
    if (!TextUtils.isEmpty(s))
    {
        httpurlconnection.setRequestProperty("User-Agent", s);
    }
    if (s1 != null)
    {
        httpurlconnection.setRequestProperty("Cookie", s1);
    }
    int i = httpurlconnection.getResponseCode();
    InputStream inputstream;
    if (i < 300 || i >= 400)
    {
        inputstream = httpurlconnection.getInputStream();
    } else
    {
        inputstream = null;
    }
    return new DoneHandlerInputStream(inputstream);
}
 
Example 6
Source File: Resolver.java    From CrappaLinks with GNU General Public License v3.0 5 votes vote down vote up
protected String doInBackground(String... urls) {
    String redirectUrl = urls[0];

    // if there's no connection, fail and return the original URL.
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(
            Context.CONNECTIVITY_SERVICE);
    if (connectivityManager.getActiveNetworkInfo() == null) {
        noConnectionError = true;
        return redirectUrl;
    }

    if (useUnshortenIt) {
        return getRedirectUsingLongURL(redirectUrl);
    } else {
        HttpURLConnection.setFollowRedirects(false);
        // Use the cookie manager so that cookies are stored. Useful for some hosts that keep
        // redirecting us indefinitely unless the set cookie is detected.
        CookieManager cookieManager = new CookieManager();
        CookieHandler.setDefault(cookieManager);

        // Should we resolve all URLs?
        boolean resolveAll = true;
        NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (resolveAllWhen.equals("NEVER") || (resolveAllWhen.equals("WIFI_ONLY") && !wifiInfo.isConnected()))
            resolveAll = false;

        // Keep trying to resolve the URL until we get a URL that isn't a redirect.
        String finalUrl = redirectUrl;
        while (redirectUrl != null && ((resolveAll) || (RedirectHelper.isRedirect(Uri.parse(redirectUrl).getHost())))) {
            redirectUrl = getRedirect(redirectUrl);
            if (redirectUrl != null) {
                // This should avoid infinite loops, just in case.
                if (redirectUrl.equals(finalUrl))
                    return finalUrl;
                finalUrl = redirectUrl;
            }
        }
        return finalUrl;
    }
}
 
Example 7
Source File: ExprUrlLastModified.java    From skUtilities with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Nullable
protected Number[] get(Event e) {
  try {
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection c = (HttpURLConnection) new URL(url.getSingle(e)).openConnection();
    c.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
    String n = String.valueOf(c.getLastModified());
    c.disconnect();
    return new Number[]{Long.valueOf(n.substring(0, 10))};
  } catch (Exception x) {
    skUtilities.prSysE("Error Reading from: '" + url.getSingle(e) + "' Is the site down?", getClass().getSimpleName(), x);
  }
  return null;
}
 
Example 8
Source File: ExprUrlOnlineState.java    From skUtilities with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Nullable
protected Boolean[] get(Event e) {
  try {
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection c = (HttpURLConnection) new URL(url.getSingle(e)).openConnection();
    c.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
    int r = c.getResponseCode();
    c.disconnect();
    return new Boolean[]{r == HttpURLConnection.HTTP_OK};
  } catch (Exception x) {
    skUtilities.prSysE("Error Reading from: '" + url.getSingle(e) + "' Is the site down?", getClass().getSimpleName(), x);
  }
  return null;
}
 
Example 9
Source File: Shibboleth3ConfService.java    From oxTrust with MIT License 5 votes vote down vote up
public boolean existsResourceUri(String resourceUrlName) {
	try {
		HttpURLConnection.setFollowRedirects(false);
		// note : you may also need
		// HttpURLConnection.setInstanceFollowRedirects(false)
		HttpURLConnection con = (HttpURLConnection) new URL(resourceUrlName).openConnection();
		con.setRequestMethod("HEAD");
		return (con.getResponseCode() == HttpURLConnection.HTTP_OK);

	} catch (Exception e) {
		log.error("existsResourceUri: {}" , resourceUrlName);
		return false;
	}
}
 
Example 10
Source File: MizLib.java    From Mizuu with Apache License 2.0 5 votes vote down vote up
public static boolean exists(String URLName){
    try {
        HttpURLConnection.setFollowRedirects(false);
        HttpURLConnection con =
                (HttpURLConnection) new URL(URLName).openConnection();
        con.setRequestMethod("HEAD");
        con.setConnectTimeout(10000);
        return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
    }
    catch (Exception e) {
        return false;
    }
}
 
Example 11
Source File: RequestBuilder.java    From jumblr with Apache License 2.0 5 votes vote down vote up
public String getRedirectUrl(String path) {
    OAuthRequest request = this.constructGet(path, null);
    sign(request);
    boolean presetVal = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    Response response = request.send();
    HttpURLConnection.setFollowRedirects(presetVal);
    if (response.getCode() == 301 || response.getCode() == 302) {
        return response.getHeader("Location");
    } else {
        throw new JumblrException(response);
    }
}
 
Example 12
Source File: TestRewriteValve.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testPermanentRedirect() throws Exception {
     // Disable the following of redirects for this test only
    boolean originalValue = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    try {
        doTestRedirect("RewriteRule ^/from/a$ /to/b [R=permanent]", "/redirect/from/a", "/redirect/to/b",
            301);
    } finally {
        HttpURLConnection.setFollowRedirects(originalValue);
    }
}
 
Example 13
Source File: Cloudflare.java    From cloudflare-scrape-Android with MIT License 4 votes vote down vote up
private void urlThread(cfCallback callback){
    mCookieManager = new CookieManager();
    mCookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); //接受所有cookies
    CookieHandler.setDefault(mCookieManager);
    HttpURLConnection.setFollowRedirects(false);
    while (!canVisit){
        if (mRetry_count>MAX_COUNT){
            break;
        }
        try {

            int responseCode = checkUrl();
            if (responseCode==200){
                canVisit=true;
                break;
            }else {
                getVisitCookie();
            }
        } catch (IOException| RuntimeException | InterruptedException e) {
            if (mCookieList!=null){
                mCookieList= new ArrayList<>(mCookieList);
                mCookieList.clear();
            }
            e.printStackTrace();
        } finally {
            closeAllConn();
        }
        mRetry_count++;
    }
    if (callback!=null){
        Looper.prepare();
        if (canVisit){
            callback.onSuccess(mCookieList,hasNewUrl,mUrl);
        }else {
            e("Get Cookie Failed");
            callback.onFail("Retries exceeded the limit");
        }


    }
}
 
Example 14
Source File: Cloudflare.java    From cloudflare-scrape-Android with MIT License 4 votes vote down vote up
private void getRedirectResponse(AnswerBean answerBean) throws IOException {
    HttpURLConnection.setFollowRedirects(false);
    mGetRedirectionConn = (HttpURLConnection) new URL(answerBean.getHost()).openConnection();

    mGetRedirectionConn.setRequestMethod(answerBean.getMethod() == AnswerBean.GET ? "GET" : "POST");
    mGetRedirectionConn.setConnectTimeout(CONN_TIMEOUT);
    mGetRedirectionConn.setReadTimeout(CONN_TIMEOUT);
    mGetRedirectionConn.setDoInput(true);
    mGetRedirectionConn.setDoOutput(true);
    mGetRedirectionConn.setUseCaches(false);
    if (!TextUtils.isEmpty(mUser_agent)){
        mGetRedirectionConn.setRequestProperty("user-agent",mUser_agent);
    }
    mGetRedirectionConn.setRequestProperty("accept",ACCEPT);
    mGetRedirectionConn.setRequestProperty("referer", mUrl);
    if (mCookieList!=null&&mCookieList.size()>0){
        mGetRedirectionConn.setRequestProperty("cookie",listToString(mCookieList));
    }
    mGetRedirectionConn.setUseCaches(false);
    if (answerBean.getMethod() == AnswerBean.POST){
        mGetRedirectionConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    }
    mGetRedirectionConn.connect();
    if (answerBean.getMethod() == AnswerBean.POST){
        StringBuilder stringBuilder = new StringBuilder();
        for(Map.Entry<String, String> entry :answerBean.getFromData().entrySet()){
            stringBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        stringBuilder.deleteCharAt(stringBuilder.length()-1);
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(mGetRedirectionConn.getOutputStream(), "UTF-8"));
        writer.write(stringBuilder.toString());
        writer.close();
    }
    switch (mGetRedirectionConn.getResponseCode()){
        case HttpURLConnection.HTTP_OK:
            mCookieList = mCookieManager.getCookieStore().getCookies();
            break;
        case HttpURLConnection.HTTP_MOVED_TEMP:
            mCookieList = mCookieManager.getCookieStore().getCookies();
            checkCookie(mCookieList);
            break;
        default:throw new IOException("getOtherResponse Code: "+
                mGetRedirectionConn.getResponseCode());
    }
}
 
Example 15
Source File: PortletIFrame.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
public boolean popupXFrame(RenderRequest request, Placement placement, String url) 
{
    if ( xframeCache < 1 ) return false;

    // Only check http:// and https:// urls
    if ( ! (url.startsWith("http://") || url.startsWith("https://")) ) return false;

    // Check the "Always POPUP" and "Always INLINE" regular expressions
    String pattern = null;
    Pattern p = null;
    Matcher m = null;
    pattern = ServerConfigurationService.getString(IFRAME_XFRAME_POPUP, null);
    if ( pattern != null && pattern.length() > 1 ) {
        p = Pattern.compile(pattern);
        m = p.matcher(url.toLowerCase());
        if ( m.find() ) {
            return true;
        }
    }
    pattern = ServerConfigurationService.getString(IFRAME_XFRAME_INLINE, null);
    if ( pattern != null && pattern.length() > 1 ) {
        p = Pattern.compile(pattern);
        m = p.matcher(url.toLowerCase());
        if ( m.find() ) {
            return false;
        }
    }

    // Don't check Local URLs
    String serverUrl = ServerConfigurationService.getServerUrl();
    if ( url.startsWith(serverUrl) ) return false;
    if ( url.startsWith(ServerConfigurationService.getAccessUrl()) ) return false;

    // Force http:// to pop-up if we are https://
    if ( request.isSecure() || ( serverUrl != null && serverUrl.startsWith("https://") ) ) {
        if ( url.startsWith("http://") ) return true;
    }

    // Check to see if time has expired...
    Date date = new Date();
    long nowTime = date.getTime();
    
    String lastTimeS = placement.getPlacementConfig().getProperty(XFRAME_LAST_TIME);
    long lastTime = -1;
    try {
        lastTime = Long.parseLong(lastTimeS);
    } catch (NumberFormatException nfe) {
        lastTime = -1;
    }

    log.debug("lastTime="+lastTime+" nowTime="+nowTime);

    if ( lastTime > 0 && nowTime < lastTime + xframeCache ) {
        String lastXF = placement.getPlacementConfig().getProperty(XFRAME_LAST_STATUS);
        log.debug("Status from placement="+lastXF);
        return "true".equals(lastXF);
    }

    placement.getPlacementConfig().setProperty(XFRAME_LAST_TIME, String.valueOf(nowTime));
    boolean retval = false;
    try {
        // note : you may also need
        //        HttpURLConnection.setInstanceFollowRedirects(false)
        HttpURLConnection.setFollowRedirects(true);
        HttpURLConnection con =
            (HttpURLConnection) new URL(url).openConnection();
        con.setRequestMethod("HEAD");

        String sakaiVersion = ServerConfigurationService.getString("version.sakai", "?");
        con.setRequestProperty("User-Agent","Java Sakai/"+sakaiVersion);

        Map headerfields = con.getHeaderFields();
        Set headers = headerfields.entrySet(); 
        for(Iterator i = headers.iterator(); i.hasNext();) { 
            Map.Entry map = (Map.Entry)i.next();
            String key = (String) map.getKey();
            if ( key == null ) continue;
            key = key.toLowerCase();
            if ( ! "x-frame-options".equals(key) ) continue;

            // Since the valid entries are SAMEORIGIN, DENY, or ALLOW-URI
            // we can pretty much assume the answer is "not us" if the header
            // is present
            retval = true;
            break;
        }

    }
    catch (Exception e) {
        // Fail pretty silently because this could be pretty chatty with bad urls and all
        log.debug(e.getMessage());
        retval = false;
    }
    placement.getPlacementConfig().setProperty(XFRAME_LAST_STATUS, String.valueOf(retval));
    // Permanently set popup to true as we don't expect that a site will go back
    if ( retval == true ) placement.getPlacementConfig().setProperty(POPUP, "true");
    placement.save();
    log.debug("Retrieved="+url+" XFrame="+retval);
    return retval;
}
 
Example 16
Source File: RedirectTest.java    From jus with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
    HttpURLConnection.setFollowRedirects(false);
    queue = Jus.newRequestQueue(null, new CustomHttpStack());
    JusLog.MarkerLog.on();
}
 
Example 17
Source File: Cloudflare.java    From cloudflare-scrape-Android with MIT License 4 votes vote down vote up
private void urlThread(cfCallback callback){
    mCookieManager = new CookieManager();
    mCookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); //接受所有cookies
    CookieHandler.setDefault(mCookieManager);
    HttpURLConnection.setFollowRedirects(false);
    while (!canVisit){
        if (mRetry_count>MAX_COUNT){
            break;
        }
        try {

            int responseCode = checkUrl();
            if (responseCode==200){
                canVisit=true;
                break;
            }else {
                getVisitCookie();
            }
        } catch (IOException | RuntimeException | InterruptedException e) {
            if (mCookieList!=null){
                mCookieList= new ArrayList<>(mCookieList);
                mCookieList.clear();
            }
            e.printStackTrace();
        } finally {
            closeAllConn();
        }
        mRetry_count++;
    }
    if (callback!=null){
        Looper.prepare();
        if (canVisit){
            callback.onSuccess(mCookieList,hasNewUrl,mUrl);
        }else {
            e("Get Cookie Failed");
            callback.onFail("Retries exceeded the limit");
        }


    }
}
 
Example 18
Source File: TestMapperWebapps.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testRedirect() throws Exception {
    // Disable the following of redirects for this test only
    boolean originalValue = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    try {
        Tomcat tomcat = getTomcatInstance();

        // Use standard test webapp as ROOT
        File rootDir = new File("test/webapp-3.0");
        org.apache.catalina.Context root =
                tomcat.addWebapp(null, "", rootDir.getAbsolutePath());

        // Add a security constraint
        SecurityConstraint constraint = new SecurityConstraint();
        SecurityCollection collection = new SecurityCollection();
        collection.addPattern("/welcome-files/*");
        collection.addPattern("/welcome-files");
        constraint.addCollection(collection);
        constraint.addAuthRole("foo");
        root.addConstraint(constraint);

        // Also make examples available
        File examplesDir = new File(getBuildDirectory(), "webapps/examples");
        org.apache.catalina.Context examples  = tomcat.addWebapp(
                null, "/examples", examplesDir.getAbsolutePath());
        examples.setMapperContextRootRedirectEnabled(false);
        // Then block access to the examples to test redirection
        RemoteAddrValve rav = new RemoteAddrValve();
        rav.setDeny(".*");
        rav.setDenyStatus(404);
        examples.getPipeline().addValve(rav);

        tomcat.start();

        // Redirects within a web application
        doRedirectTest("/welcome-files", 401);
        doRedirectTest("/welcome-files/", 401);

        doRedirectTest("/jsp", 302);
        doRedirectTest("/jsp/", 404);

        doRedirectTest("/WEB-INF", 404);
        doRedirectTest("/WEB-INF/", 404);

        // Redirects between web applications
        doRedirectTest("/examples", 404);
        doRedirectTest("/examples/", 404);
    } finally {
        HttpURLConnection.setFollowRedirects(originalValue);
    }
}
 
Example 19
Source File: ApigeeHttpURLConnection.java    From apigee-android-sdk with Apache License 2.0 4 votes vote down vote up
public static void setFollowRedirects(boolean auto)
{
	HttpURLConnection.setFollowRedirects(auto);
}
 
Example 20
Source File: AccessTokenUtils.java    From gitlab4j-api with MIT License 4 votes vote down vote up
/**
 * Fetches the GitLab health check access token using HTML scraping.
 *
 * @param baseUrl the GitLab server base URL
 * @param username the user name of an admin user to log in with
 * @param password the password of the provided username
 * @return the fetched health check access token
 * @throws GitLabApiException if any exception occurs
 */
public static final String getHealthCheckAccessToken(final String baseUrl, final String username,
        final String password) throws GitLabApiException {

    // Save the follow redirect state so it can be restored later
    boolean savedFollowRedirects = HttpURLConnection.getFollowRedirects();
    String cookies = null;

    try {

        // Must manually follow redirects
        if (savedFollowRedirects) {
            HttpURLConnection.setFollowRedirects(false);
        }

        /*******************************************************************************
         * Step 1: Login and get the session cookie. *
         *******************************************************************************/
        cookies = login(baseUrl, username, password);

        /*******************************************************************************
         * Step 2: Go to the /admin/health_check page and fetch the * health check
         * access token. *
         *******************************************************************************/
        String urlString = baseUrl + "/admin/health_check";
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("User-Agent", USER_AGENT);
        connection.setRequestProperty("Cookie", cookies);
        connection.setReadTimeout(10000);
        connection.setConnectTimeout(10000);

        // Make sure the response code is 200, otherwise there is a failure
        int responseCode = connection.getResponseCode();
        if (responseCode != 200) {
            throw new GitLabApiException("Failure loading Health Check page, aborting!");
        }

        // Extract the personal access token from the page and return it
        String content = getContent(connection);
        Matcher matcher = HEALTH_CHECK_ACCESS_TOKEN_PATTERN.matcher(content);
        if (!matcher.find()) {
            throw new GitLabApiException("health-check-access-token not found, aborting!");
        }

        String healthCheckAccessToken = matcher.group(1);
        return (healthCheckAccessToken);

    } catch (IOException ioe) {
        throw new GitLabApiException(ioe);
    } finally {

        if (cookies != null) {
            try { logout(baseUrl, cookies); } catch (Exception ignore) {}
        }

        if (savedFollowRedirects) {
            HttpURLConnection.setFollowRedirects(true);
        }
    }
}