java.net.HttpURLConnection#setFollowRedirects ( )源码实例Demo

下面列出了java.net.HttpURLConnection#setFollowRedirects ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: ChatGameFontificator   文件: LazyLoadEmoji.java
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;
    }
}
 
源代码2 项目: che   文件: OsioKeycloakTestAuthServiceClient.java
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;
}
 
源代码3 项目: webdsl   文件: PollServerAndRestart.java
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;		
}
 
源代码4 项目: MiBandDecompiled   文件: Network.java
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);
}
 
源代码5 项目: Tomcat8-Source-Read   文件: TestRewriteValve.java
@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);
    }
}
 
源代码6 项目: CrappaLinks   文件: Resolver.java
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;
    }
}
 
源代码7 项目: jumblr   文件: RequestBuilder.java
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);
    }
}
 
源代码8 项目: Mizuu   文件: MizLib.java
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;
    }
}
 
源代码9 项目: oxTrust   文件: Shibboleth3ConfService.java
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;
	}
}
 
源代码10 项目: skUtilities   文件: ExprUrlOnlineState.java
@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;
}
 
源代码11 项目: skUtilities   文件: ExprUrlLastModified.java
@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;
}
 
源代码12 项目: geowave   文件: DynamoDBLocal.java
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;
}
 
源代码13 项目: cloudflare-scrape-Android   文件: Cloudflare.java
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");
        }


    }
}
 
源代码14 项目: jus   文件: RedirectTest.java
@Before
public void setup() {
    HttpURLConnection.setFollowRedirects(false);
    queue = Jus.newRequestQueue(null, new CustomHttpStack());
    JusLog.MarkerLog.on();
}
 
源代码15 项目: cloudflare-scrape-Android   文件: Cloudflare.java
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");
        }


    }
}
 
源代码16 项目: cloudflare-scrape-Android   文件: Cloudflare.java
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());
    }
}
 
源代码17 项目: sakai   文件: PortletIFrame.java
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;
}
 
源代码18 项目: tomcatsrc   文件: TestMapperWebapps.java
@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);
    }
}
 
public static void setFollowRedirects(boolean auto)
{
	HttpURLConnection.setFollowRedirects(auto);
}
 
源代码20 项目: gitlab4j-api   文件: AccessTokenUtils.java
/**
 * 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);
        }
    }
}