本文整理汇总了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();
}
示例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();
}
示例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);
}
示例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);
}
}
示例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;
}
}
示例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);
}
示例7: tearDown
import java.net.HttpURLConnection; //导入方法依赖的package包/类
@After
public void tearDown() throws IOException {
HttpURLConnection.setFollowRedirects(defaultFollowRedirects);
mockWebServer.shutdown();
}
示例8: disableRedirects
import java.net.HttpURLConnection; //导入方法依赖的package包/类
@Override
public void disableRedirects() {
validateNotStarted();
HttpURLConnection.setFollowRedirects(false);
}
示例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);
}
}
示例10: setFollowRedirects
import java.net.HttpURLConnection; //导入方法依赖的package包/类
public static void setFollowRedirects(boolean followRedirect) {
HttpURLConnection.setFollowRedirects(followRedirect);
}