本文整理汇总了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();
}