當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpURLConnection.setFollowRedirects方法代碼示例

本文整理匯總了Java中java.net.HttpURLConnection.setFollowRedirects方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpURLConnection.setFollowRedirects方法的具體用法?Java HttpURLConnection.setFollowRedirects怎麽用?Java HttpURLConnection.setFollowRedirects使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.net.HttpURLConnection的用法示例。


在下文中一共展示了HttpURLConnection.setFollowRedirects方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testDisableRedirectsGlobal

import java.net.HttpURLConnection; //導入方法依賴的package包/類
@SmallTest
@Feature({"Cronet"})
@CompareDefaultWithCronet
public void testDisableRedirectsGlobal() throws Exception {
    HttpURLConnection.setFollowRedirects(false);
    URL url = new URL(NativeTestServer.getFileURL("/redirect.html"));
    HttpURLConnection connection =
            (HttpURLConnection) url.openConnection();
    // Redirect following control broken in Android Marshmallow:
    // https://code.google.com/p/android/issues/detail?id=194495
    if (!testingSystemHttpURLConnection() || Build.VERSION.SDK_INT != Build.VERSION_CODES.M) {
        assertEquals(302, connection.getResponseCode());
        assertEquals("Found", connection.getResponseMessage());
        assertEquals("/success.txt", connection.getHeaderField("Location"));
        assertEquals(
                NativeTestServer.getFileURL("/redirect.html"), connection.getURL().toString());
    }
    connection.disconnect();
}
 
開發者ID:lizhangqu,項目名稱:chromium-net-for-android,代碼行數:20,代碼來源:CronetHttpURLConnectionTest.java

示例2: testDisableRedirectsGlobalAfterConnectionIsCreated

import java.net.HttpURLConnection; //導入方法依賴的package包/類
@SmallTest
@Feature({"Cronet"})
@CompareDefaultWithCronet
public void testDisableRedirectsGlobalAfterConnectionIsCreated()
        throws Exception {
    HttpURLConnection.setFollowRedirects(true);
    URL url = new URL(NativeTestServer.getFileURL("/redirect.html"));
    HttpURLConnection connection =
            (HttpURLConnection) url.openConnection();
    // Disabling redirects globally after creating the HttpURLConnection
    // object should have no effect on the request.
    HttpURLConnection.setFollowRedirects(false);
    assertEquals(200, connection.getResponseCode());
    assertEquals("OK", connection.getResponseMessage());
    assertEquals(NativeTestServer.getFileURL("/success.txt"),
            connection.getURL().toString());
    assertEquals("this is a text file\n", TestUtil.getResponseAsString(connection));
    connection.disconnect();
}
 
開發者ID:lizhangqu,項目名稱:chromium-net-for-android,代碼行數:20,代碼來源:CronetHttpURLConnectionTest.java

示例3: setUp

import java.net.HttpURLConnection; //導入方法依賴的package包/類
@Before
public void setUp() throws IOException {
  MockitoAnnotations.initMocks(this);
  defaultFollowRedirects = HttpURLConnection.getFollowRedirects();
  HttpURLConnection.setFollowRedirects(false);
  mockWebServer = new MockWebServer();
  mockWebServer.start();

  streamCaptor = ArgumentCaptor.forClass(InputStream.class);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:11,代碼來源:HttpUrlFetcherServerTest.java

示例4: testRedirect

import java.net.HttpURLConnection; //導入方法依賴的package包/類
@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);
    }
}
 
開發者ID:liaokailin,項目名稱:tomcat7,代碼行數:53,代碼來源:TestMapperWebapps.java

示例5: checkURL

import java.net.HttpURLConnection; //導入方法依賴的package包/類
/**
 * Checks if a URL exists and can respond to an HTTP request.
 * @param url The URL to check.
 * @return True if the URL exists, false if it doesn't or an error occured.
 */
public static boolean checkURL(String url) {
	try {
		HttpURLConnection.setFollowRedirects(false);
		HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
		con.setRequestMethod("HEAD");
		return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
	} catch (Exception e) {
		return false;
	}
}
 
開發者ID:Tisawesomeness,項目名稱:Minecord,代碼行數:16,代碼來源:RequestUtils.java

示例6: testDataNodeRedirect

import java.net.HttpURLConnection; //導入方法依賴的package包/類
private void testDataNodeRedirect(Path path) throws IOException {
  // Create the file
  if (hdfs.exists(path)) {
    hdfs.delete(path, true);
  }
  FSDataOutputStream out = hdfs.create(path, (short) 1);
  out.writeBytes("0123456789");
  out.close();

  // Get the path's block location so we can determine
  // if we were redirected to the right DN.
  BlockLocation[] locations = hdfs.getFileBlockLocations(path, 0, 10);
  String xferAddr = locations[0].getNames()[0];

  // Connect to the NN to get redirected
  URL u = hftpFs.getNamenodeURL(
      "/data" + ServletUtil.encodePath(path.toUri().getPath()),
      "ugi=userx,groupy");
  HttpURLConnection conn = (HttpURLConnection) u.openConnection();
  HttpURLConnection.setFollowRedirects(true);
  conn.connect();
  conn.getInputStream();

  boolean checked = false;
  // Find the datanode that has the block according to locations
  // and check that the URL was redirected to this DN's info port
  for (DataNode node : cluster.getDataNodes()) {
    DatanodeRegistration dnR = DataNodeTestUtils.getDNRegistrationForBP(node,
        blockPoolId);
    if (dnR.getXferAddr().equals(xferAddr)) {
      checked = true;
      assertEquals(dnR.getInfoPort(), conn.getURL().getPort());
    }
  }
  assertTrue("The test never checked that location of "
      + "the block and hftp desitnation are the same", checked);
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:38,代碼來源:TestHftpFileSystem.java

示例7: tearDown

import java.net.HttpURLConnection; //導入方法依賴的package包/類
@After
public void tearDown() throws IOException {
  HttpURLConnection.setFollowRedirects(defaultFollowRedirects);
  mockWebServer.shutdown();
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:6,代碼來源:HttpUrlFetcherServerTest.java

示例8: disableRedirects

import java.net.HttpURLConnection; //導入方法依賴的package包/類
@Override
public void disableRedirects() {
    validateNotStarted();
    HttpURLConnection.setFollowRedirects(false);
}
 
開發者ID:lizhangqu,項目名稱:chromium-net-for-android,代碼行數:6,代碼來源:HttpUrlConnectionUrlRequest.java

示例9: downloadXmlAsStream

import java.net.HttpURLConnection; //導入方法依賴的package包/類
private static InputStream downloadXmlAsStream(Context context, URL url, String userAgent,
                                               String cookie, Map<String, String>
                                                       requestHdrs, HttpHeaderInfo
                                                       responseHdrs) throws IOException {
    if (context == null) {
        throw new IllegalArgumentException("context");
    } else if (url == null) {
        throw new IllegalArgumentException("url");
    } else {
        InputStream inputStream;
        URL newUrl = url;
        HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier);
        HttpURLConnection.setFollowRedirects(true);
        HttpURLConnection conn = getHttpUrlConnection(context, newUrl);
        conn.setConnectTimeout(10000);
        conn.setReadTimeout(15000);
        TrustManager[] tm = new TrustManager[]{ignoreCertificationTrustManger};
        SSLContext sslContext = null;
        try {
            sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, tm, new SecureRandom());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e2) {
            e2.printStackTrace();
        }
        ((HttpsURLConnection) conn).setSSLSocketFactory(sslContext.getSocketFactory());
        if (!TextUtils.isEmpty(userAgent)) {
            conn.setRequestProperty(USER_AGENT, userAgent);
        }
        if (cookie != null) {
            conn.setRequestProperty("Cookie", cookie);
        }
        if (requestHdrs != null) {
            for (String key : requestHdrs.keySet()) {
                conn.setRequestProperty(key, (String) requestHdrs.get(key));
            }
        }
        if (responseHdrs != null && (url.getProtocol().equals("http") || url.getProtocol()
                .equals(b.a))) {
            responseHdrs.ResponseCode = conn.getResponseCode();
            if (responseHdrs.AllHeaders == null) {
                responseHdrs.AllHeaders = new HashMap();
            }
            int i = 0;
            while (true) {
                String name = conn.getHeaderFieldKey(i);
                String value = conn.getHeaderField(i);
                if (name == null && value == null) {
                    break;
                }
                if (!(TextUtils.isEmpty(name) || TextUtils.isEmpty(value))) {
                    responseHdrs.AllHeaders.put(name, value);
                }
                i++;
            }
        }
        try {
            inputStream = conn.getInputStream();
        } catch (IOException e3) {
            inputStream = conn.getErrorStream();
        }
        return new DoneHandlerInputStream(inputStream);
    }
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:66,代碼來源:Network.java

示例10: setFollowRedirects

import java.net.HttpURLConnection; //導入方法依賴的package包/類
public static void setFollowRedirects(boolean followRedirect) {
	HttpURLConnection.setFollowRedirects(followRedirect);
}
 
開發者ID:haducloc,項目名稱:appslandia-sweetsop,代碼行數:4,代碼來源:HttpClient.java


注:本文中的java.net.HttpURLConnection.setFollowRedirects方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。