本文整理匯總了Java中org.apache.http.client.protocol.HttpClientContext.getRedirectLocations方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpClientContext.getRedirectLocations方法的具體用法?Java HttpClientContext.getRedirectLocations怎麽用?Java HttpClientContext.getRedirectLocations使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.client.protocol.HttpClientContext
的用法示例。
在下文中一共展示了HttpClientContext.getRedirectLocations方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: resolveHttpRedirects
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
/** Return final location from http redirects */
public static String resolveHttpRedirects(String uri)
throws IOException, URISyntaxException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpClientContext context = HttpClientContext.create();
HttpGet httpget = new HttpGet(uri);
//httpget.addHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20121202 Firefox/17.0 Iceweasel/17.0.1");
CloseableHttpResponse response = httpclient.execute(httpget, context);
try {
HttpHost target = context.getTargetHost();
List<URI> redirectLocations = context.getRedirectLocations();
URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);
return location.toString();
} finally {
response.close();
}
}
示例2: retrieveAccessToken
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private void retrieveAccessToken() throws IOException, WeiboClientException {
String state = "__MY_STATE__";
String authorizationCallback = "https://api.weibo.com/oauth2/authorize";
String url = this.client.getAuthorizationUrl(ResponseType.Code, DisplayType.Default, state, authorizationCallback);
//httpget
CloseableHttpResponse response = null;
HttpClientContext context = HttpClientContext.create();
HttpGet httpGet = new HttpGet(url);
response = httpClient.execute(httpGet, context);
// 獲取所有的重定向位置
List<URI> redirectLocations = context.getRedirectLocations();
//end
System.out.println("Please visit: " + url);
System.out.print("Input code: ");
in = new BufferedReader(new InputStreamReader(System.in));
String code = in.readLine();
String accessTokenCallback = "https://api.weibo.com/oauth2/authorize";
SinaWeibo2AccessToken accessToken = this.client.getAccessTokenByCode(code, accessTokenCallback);
System.out.println();
System.out.println("Access token: " + accessToken.getToken());
System.out.println("Uid: " + accessToken.getUid());
System.out.println("Expires in: " + accessToken.getExpiresIn());
System.out.println("Remind in: " + accessToken.getRemindIn());
accessToken = new SinaWeibo2AccessToken(accessToken.getToken());
this.client.setAccessToken(accessToken);
}
示例3: testUrlRedirect
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private static void testUrlRedirect() throws IOException, URISyntaxException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpClientContext context = HttpClientContext.create();
//String uri="http://feeds.nbcnews.com/c/35002/f/663303/s/410be3e0/sc/1/l/0L0Snbcnews0N0Cpolitics0Cfirst0Eread0Csimple0Ecommon0Esense0Ejeh0Ejohnson0Edefends0Eexecutive0Eaction0Eimmigration0En259736/story01.htm";
//String uri="http://feeds.theguardian.com/c/34708/f/663879/s/410c0702/sc/8/l/0L0Stheguardian0N0Cworld0C20A140Cdec0C0A20Cnorth0Ekorea0Esony0Ecyber0Eattack/story01.htm";
//String uri = "http://feeds.reuters.com/~r/Reuters/worldNews/~3/ySVJ_LFYBrs/story01.htm";
String uri = "http://rss.nytimes.com/c/34625/f/642565/s/40f27c2a/sc/20/l/0L0Snytimes0N0C20A140C110C290Cworld0Cmiddleeast0Cpalestinian0Ehaven0Efor0E60Edecades0Enow0Eflooded0Efrom0Esyria0E0Bhtml0Dpartner0Frss0Gemc0Frss/story01.htm";
HttpGet httpget = new HttpGet(uri);
httpget.addHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20121202 Firefox/17.0 Iceweasel/17.0.1");
CloseableHttpResponse response = httpclient.execute(httpget, context);
try {
HttpHost target = context.getTargetHost();
System.out.println("httpget URI: " + httpget.getURI());
System.out.println("target host: " + target);
System.out.println(response.getStatusLine().getStatusCode());
for (Header h : response.getAllHeaders()) {
System.out.println(h.getName()+ " : " + h.getValue());
}
// BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
// String line = null;
// while ((line = br.readLine()) != null) {
// System.out.println(line);
// }
List<URI> redirectLocations = context.getRedirectLocations();
if (redirectLocations != null)
for (URI loc : redirectLocations) {
System.out.println("redirect location: " + loc);
}
URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);
System.out.println("Final HTTP location: " + location.toASCIIString());
// Expected to be an absolute URI
} finally {
response.close();
}
}
示例4: testAbsoluteRequestURIWithFragment
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testAbsoluteRequestURIWithFragment() throws Exception {
this.serverBootstrap.registerHandler("*", new SimpleService());
final HttpHost target = start();
final URI uri = new URIBuilder()
.setHost(target.getHostName())
.setPort(target.getPort())
.setScheme(target.getSchemeName())
.setPath("/stuff")
.setFragment("blahblah")
.build();
final HttpGet httpget = new HttpGet(uri);
final HttpClientContext context = HttpClientContext.create();
final HttpResponse response = this.httpclient.execute(httpget, context);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
EntityUtils.consume(response.getEntity());
final HttpRequest request = context.getRequest();
Assert.assertEquals("/stuff", request.getRequestLine().getUri());
final List<URI> redirectLocations = context.getRedirectLocations();
final URI location = URIUtils.resolve(uri, target, redirectLocations);
Assert.assertEquals(uri, location);
}
示例5: testBasicRedirect301
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testBasicRedirect301() throws Exception {
this.serverBootstrap.registerHandler("*",
new BasicRedirectService(HttpStatus.SC_MOVED_PERMANENTLY));
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final HttpGet httpget = new HttpGet("/oldlocation/");
final HttpResponse response = this.httpclient.execute(target, httpget, context);
EntityUtils.consume(response.getEntity());
final HttpRequest reqWrapper = context.getRequest();
final HttpHost host = context.getTargetHost();
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
Assert.assertEquals(target, host);
final List<URI> redirects = context.getRedirectLocations();
Assert.assertNotNull(redirects);
Assert.assertEquals(1, redirects.size());
final URI redirect = URIUtils.rewriteURI(new URI("/newlocation/"), target);
Assert.assertTrue(redirects.contains(redirect));
}
示例6: testGetLocationUriAllowCircularRedirects
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testGetLocationUriAllowCircularRedirects() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
final RequestConfig config = RequestConfig.custom().setCircularRedirectsAllowed(true).build();
context.setRequestConfig(config);
final URI uri1 = URI.create("http://localhost/stuff1");
final URI uri2 = URI.create("http://localhost/stuff2");
final URI uri3 = URI.create("http://localhost/stuff3");
final HttpGet httpget1 = new HttpGet("http://localhost/");
final HttpResponse response1 = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response1.addHeader("Location", uri1.toASCIIString());
final HttpGet httpget2 = new HttpGet(uri1.toASCIIString());
final HttpResponse response2 = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response2.addHeader("Location", uri2.toASCIIString());
final HttpGet httpget3 = new HttpGet(uri2.toASCIIString());
final HttpResponse response3 = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response3.addHeader("Location", uri3.toASCIIString());
Assert.assertEquals(uri1, redirectStrategy.getLocationURI(httpget1, response1, context));
Assert.assertEquals(uri2, redirectStrategy.getLocationURI(httpget2, response2, context));
Assert.assertEquals(uri3, redirectStrategy.getLocationURI(httpget3, response3, context));
final List<URI> uris = context.getRedirectLocations();
Assert.assertNotNull(uris);
Assert.assertTrue(uris.contains(uri1));
Assert.assertTrue(uris.contains(uri2));
Assert.assertTrue(uris.contains(uri3));
Assert.assertEquals(3, uris.size());
Assert.assertEquals(uri1, uris.get(0));
Assert.assertEquals(uri2, uris.get(1));
Assert.assertEquals(uri3, uris.get(2));
}
示例7: testBasicRedirect300
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testBasicRedirect300() throws Exception {
this.serverBootstrap.registerHandler("*",
new BasicRedirectService(HttpStatus.SC_MULTIPLE_CHOICES));
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final HttpGet httpget = new HttpGet("/oldlocation/");
final HttpResponse response = this.httpclient.execute(target, httpget, context);
EntityUtils.consume(response.getEntity());
final HttpRequest reqWrapper = context.getRequest();
Assert.assertEquals(HttpStatus.SC_MULTIPLE_CHOICES, response.getStatusLine().getStatusCode());
Assert.assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
final List<URI> redirects = context.getRedirectLocations();
Assert.assertNull(redirects);
}