下面列出了java.net.HttpURLConnection#setFollowRedirects ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
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;
}
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;
}
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);
}
@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);
}
}
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;
}
}
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);
}
}
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;
}
}
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;
}
}
@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;
}
@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;
}
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;
}
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");
}
}
}
@Before
public void setup() {
HttpURLConnection.setFollowRedirects(false);
queue = Jus.newRequestQueue(null, new CustomHttpStack());
JusLog.MarkerLog.on();
}
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");
}
}
}
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());
}
}
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;
}
@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);
}
/**
* 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);
}
}
}