本文整理匯總了Java中org.apache.http.client.protocol.HttpClientContext.create方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpClientContext.create方法的具體用法?Java HttpClientContext.create怎麽用?Java HttpClientContext.create使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.client.protocol.HttpClientContext
的用法示例。
在下文中一共展示了HttpClientContext.create方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: connect
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
public Map<String, String> connect(String url, Map<String, Object> parameters) throws ManagerResponseException {
Map<String, String> response = new HashMap<String, String>();
CloseableHttpClient httpclient = HttpClients.createDefault();
List<NameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair("j_username", (String) parameters.get("login")));
nvps.add(new BasicNameValuePair("j_password", (String) parameters.get("password")));
localContext = HttpClientContext.create();
localContext.setCookieStore(new BasicCookieStore());
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse httpResponse = httpclient.execute(httpPost, localContext);
ResponseHandler<String> handler = new CustomResponseErrorHandler();
String body = handler.handleResponse(httpResponse);
response.put(BODY, body);
httpResponse.close();
} catch (Exception e) {
authentificationUtils.getMap().clear();
throw new ManagerResponseException(e.getMessage(), e);
}
return response;
}
示例3: testBasicRedirect302
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testBasicRedirect302() throws Exception {
this.serverBootstrap.registerHandler("*",
new BasicRedirectService(HttpStatus.SC_MOVED_TEMPORARILY));
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);
}
示例4: testAuthenticationUserinfoInRedirectSuccess
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testAuthenticationUserinfoInRedirectSuccess() throws Exception {
this.serverBootstrap.registerHandler("*", new AuthHandler());
this.serverBootstrap.registerHandler("/thatway", new RedirectHandler());
final HttpHost target = start();
final HttpGet httpget = new HttpGet("http://" + target.toHostString() + "/thatway");
final HttpClientContext context = HttpClientContext.create();
final HttpResponse response = this.httpclient.execute(target, httpget, context);
final HttpEntity entity = response.getEntity();
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertNotNull(entity);
EntityUtils.consume(entity);
}
示例5: testRelativeRequestURIWithFragment
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testRelativeRequestURIWithFragment() throws Exception {
this.serverBootstrap.registerHandler("*", new SimpleService());
final HttpHost target = start();
final HttpGet httpget = new HttpGet("/stuff#blahblah");
final HttpClientContext context = HttpClientContext.create();
final HttpResponse response = this.httpclient.execute(target, 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());
}
示例6: testNTLMv1Type2Message
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testNTLMv1Type2Message() throws Exception {
this.serverBootstrap.registerHandler("*", new NtlmType2MessageResponseHandler("TlRMTVNTUAACAA" +
"AADAAMADgAAAAzggLiASNFZ4mrze8AAAAAAAAAAAAAAAAAAAAABgBwFwAAAA9T" +
"AGUAcgB2AGUAcgA="));
final HttpHost target = start();
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new NTCredentials("test", "test", null, null));
this.httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
final HttpContext context = HttpClientContext.create();
final HttpGet httpget = new HttpGet("/");
final HttpResponse response = this.httpclient.execute(target, httpget, context);
EntityUtils.consume(response.getEntity());
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED,
response.getStatusLine().getStatusCode());
}
示例7: configureContext
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private HttpClientContext configureContext(CredentialsProvider credentialsProvider, String url) {
if (credentialsProvider != null) {
try {
URL targetUrl = new URL(url);
HttpHost targetHost =
new HttpHost(targetUrl.getHost(), targetUrl.getPort(), targetUrl.getProtocol());
AuthCache authCache = new BasicAuthCache();
authCache.put(targetHost, new BasicScheme());
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProvider);
context.setAuthCache(authCache);
return context;
} catch (MalformedURLException e) {
LOG.error("Cannot parse URL '{}'", url, e);
}
}
return null;
}
示例8: testPostNoRedirect
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testPostNoRedirect() throws Exception {
this.serverBootstrap.registerHandler("*", new BasicRedirectService());
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final HttpPost httppost = new HttpPost("/oldlocation/");
httppost.setEntity(new StringEntity("stuff"));
final HttpResponse response = this.httpclient.execute(target, httppost, context);
EntityUtils.consume(response.getEntity());
final HttpRequest reqWrapper = context.getRequest();
Assert.assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, response.getStatusLine().getStatusCode());
Assert.assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
Assert.assertEquals("POST", reqWrapper.getRequestLine().getMethod());
}
示例9: testRelativeRedirect2
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testRelativeRedirect2() throws Exception {
this.serverBootstrap.registerHandler("*", new RelativeRedirectService2());
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final RequestConfig config = RequestConfig.custom().setRelativeRedirectsAllowed(true).build();
final HttpGet httpget = new HttpGet("/test/oldlocation");
httpget.setConfig(config);
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("/test/relativelocation", reqWrapper.getRequestLine().getUri());
Assert.assertEquals(host, target);
}
示例10: MarkLogicWriter
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
public MarkLogicWriter(final Map<String, String> config){
connectionUrl = config.get(MarkLogicSinkConfig.CONNECTION_URL);
user = config.get(MarkLogicSinkConfig.CONNECTION_USER);
password = config.get(MarkLogicSinkConfig.CONNECTION_PASSWORD);
requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5 * 1000).build();
localContext = HttpClientContext.create();
httpClient = HttpClientBuilder.create().build();
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
localContext.setCredentialsProvider(credentialsProvider);
localContext.setRequestConfig(requestConfig);
}
示例11: get
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
protected HttpResponse get(String url) throws ClientProtocolException, IOException, URISyntaxException {
final CloseableHttpClient httpClient = HttpClients.createDefault();
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "admin"));
final HttpClientContext localContext = HttpClientContext.create();
localContext.setCredentialsProvider(credentialsProvider);
return httpClient.execute(createGet(url), localContext);
}
示例12: delete
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
protected HttpResponse delete(String url) throws ClientProtocolException, IOException, URISyntaxException{
final CloseableHttpClient httpClient = HttpClients.createDefault();
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "admin"));
final HttpClientContext localContext = HttpClientContext.create();
localContext.setCredentialsProvider(credentialsProvider);
return httpClient.execute(createDelete(url), localContext);
}
示例13: createContext
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private HttpClientContext createContext(Authentication auth, HttpHost target) {
HttpClientContext httpClientContext = HttpClientContext.create();
CookieStore cookieStore = new BasicCookieStore();
httpClientContext.setCookieStore(cookieStore);
if (auth.usePreemptiveAuthentication()) {
httpClientContext.setAuthCache(new Auth().getAuthCache(auth, target));
}
return httpClientContext;
}
示例14: 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);
}
示例15: init
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private static void init() {
context = HttpClientContext.create();
cookieStore = new BasicCookieStore();
// 配置超時時間(連接服務端超時1秒,請求數據返回超時2秒)
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(120000).setSocketTimeout(60000)
.setConnectionRequestTimeout(60000).build();
// 設置默認跳轉以及存儲cookie
httpClient = HttpClientBuilder.create()
.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
.setRedirectStrategy(new DefaultRedirectStrategy()).setDefaultRequestConfig(requestConfig)
.setDefaultCookieStore(cookieStore).build();
}