use of org.apache.http.client.protocol.HttpClientContext in project swift by luastar.
the class HttpClientUtils method getRedirectUrl.
public static String getRedirectUrl(String url, Map<String, String> headMap) {
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
try {
HttpGet httpGet = new HttpGet(url);
// 超时设置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(DEFAULT_TIMEOUT).setConnectTimeout(DEFAULT_TIMEOUT).setConnectionRequestTimeout(DEFAULT_TIMEOUT).build();
httpGet.setConfig(requestConfig);
// head设置
if (headMap != null && !headMap.isEmpty()) {
for (Map.Entry<String, String> entry : headMap.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
// 自定义重定向,不自动处理
CustomRedirectStrategy redirectStrategy = new CustomRedirectStrategy();
httpclient = createHttpClient(url, redirectStrategy);
HttpClientContext context = HttpClientContext.create();
long start = System.currentTimeMillis();
response = httpclient.execute(httpGet, context);
long end = System.currentTimeMillis();
int status = response.getStatusLine().getStatusCode();
logger.info("请求url:{},结果状态:{},耗时:{}毫秒。", url, status, ((end - start)));
HttpEntity entity = response.getEntity();
logger.info(EntityUtils.toString(entity));
// 判断是否重定向
boolean isRedirected = redirectStrategy.isRedirectedDefault(httpGet, response, context);
if (!isRedirected) {
return null;
}
return redirectStrategy.getRedirectLocation(httpGet, response, context);
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
org.apache.http.client.utils.HttpClientUtils.closeQuietly(response);
org.apache.http.client.utils.HttpClientUtils.closeQuietly(httpclient);
}
return null;
}
use of org.apache.http.client.protocol.HttpClientContext in project camel by apache.
the class HttpPollingConsumer method doReceive.
protected Exchange doReceive(int timeout) {
Exchange exchange = endpoint.createExchange();
HttpRequestBase method = createMethod(exchange);
HttpClientContext httpClientContext = new HttpClientContext();
// set optional timeout in millis
if (timeout > 0) {
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).build();
httpClientContext.setRequestConfig(requestConfig);
}
HttpEntity responeEntity = null;
try {
// execute request
HttpResponse response = httpClient.execute(method, httpClientContext);
int responseCode = response.getStatusLine().getStatusCode();
responeEntity = response.getEntity();
Object body = HttpHelper.readResponseBodyFromInputStream(responeEntity.getContent(), exchange);
// lets store the result in the output message.
Message message = exchange.getOut();
message.setBody(body);
// lets set the headers
Header[] headers = response.getAllHeaders();
HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy();
for (Header header : headers) {
String name = header.getName();
// mapping the content-type
if (name.toLowerCase().equals("content-type")) {
name = Exchange.CONTENT_TYPE;
}
String value = header.getValue();
if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) {
message.setHeader(name, value);
}
}
message.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
if (response.getStatusLine() != null) {
message.setHeader(Exchange.HTTP_RESPONSE_TEXT, response.getStatusLine().getReasonPhrase());
}
return exchange;
} catch (IOException e) {
throw new RuntimeCamelException(e);
} finally {
if (responeEntity != null) {
try {
EntityUtils.consume(responeEntity);
} catch (IOException e) {
// nothing what we can do
}
}
}
}
use of org.apache.http.client.protocol.HttpClientContext in project estatio by estatio.
the class UrlDownloaderUsingNtlmCredentials method download.
@Override
public byte[] download(final URL url) throws IOException {
HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
// Make sure the same context is used to execute logically related requests
// (not thread-safe, so need a new one each time)
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
HttpGet httpGet = new HttpGet(url.getFile());
try (final CloseableHttpResponse httpResponse = httpclient.execute(target, httpGet, context)) {
final StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine == null) {
throw new ApplicationException(String.format("Could not obtain response statusLine for %s", url.toExternalForm()));
}
final int statusCode = statusLine.getStatusCode();
if (statusCode != 200) {
// try to read content of entity, but ignore any exceptions
// because we are simply trying to get extra data to report in the exception below
InputStreamReader inputStreamReader = null;
String entityContent = "";
try {
inputStreamReader = new InputStreamReader(httpResponse.getEntity().getContent());
entityContent = CharStreams.toString(inputStreamReader);
} catch (java.lang.Throwable ex) {
// ignore
} finally {
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (Exception ex) {
// ignore
}
}
}
throw new ApplicationException(String.format("Failed to download from '%s': %d %s\n%s", url.toExternalForm(), statusCode, statusLine.getReasonPhrase(), entityContent));
}
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
httpResponse.getEntity().writeTo(baos);
return baos.toByteArray();
}
}
use of org.apache.http.client.protocol.HttpClientContext in project briefcase by opendatakit.
the class WebUtils method createHttpContext.
public static HttpClientContext createHttpContext() {
// set up one context for all HTTP requests so that authentication
// and cookies can be retained.
HttpClientContext localContext = HttpClientContext.create();
// establish a local cookie store for this attempt at downloading...
CookieStore cookieStore = new BasicCookieStore();
localContext.setCookieStore(cookieStore);
// and establish a credentials provider...
CredentialsProvider credsProvider = new BasicCredentialsProvider();
localContext.setCredentialsProvider(credsProvider);
return localContext;
}
use of org.apache.http.client.protocol.HttpClientContext in project briefcase by opendatakit.
the class AggregateUtils method commonDownloadFile.
/**
* Common routine to download a document from the downloadUrl and save the
* contents in the file 'f'. Shared by media file download and form file
* download.
*
* @param f
* @param downloadUrl
* @throws URISyntaxException
* @throws IOException
* @throws ClientProtocolException
* @throws TransmissionException
*/
public static final void commonDownloadFile(ServerConnectionInfo serverInfo, File f, String downloadUrl) throws URISyntaxException, IOException, TransmissionException {
log.info("Downloading URL {} into {}", downloadUrl, f);
// OK. We need to download it because we either:
// (1) don't have it
// (2) don't know if it is changed because the hash is not md5
// (3) know it is changed
URI u = null;
try {
log.info("Parsing URL {}", downloadUrl);
URL uurl = new URL(downloadUrl);
u = uurl.toURI();
} catch (MalformedURLException | URISyntaxException e) {
log.warn("bad download url", e);
throw e;
}
HttpClient httpclient = WebUtils.createHttpClient();
// get shared HttpContext so that authentication and cookies are retained.
HttpClientContext localContext = WebUtils.getHttpContext();
// set up request...
HttpGet req = WebUtils.createOpenRosaHttpGet(u);
WebUtils.setCredentials(localContext, serverInfo, u);
HttpResponse response = null;
// try
{
response = httpclient.execute(req, localContext);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 401) {
// We reset the Http context to force next request to authenticate itself
WebUtils.resetHttpContext();
throw new TransmissionException("Authentication failure");
} else if (statusCode != 200) {
String errMsg = "Fetch failed. Detailed reason: " + response.getStatusLine().getReasonPhrase() + " (" + statusCode + ")";
log.error(errMsg);
flushEntityBytes(response.getEntity());
throw new TransmissionException(errMsg);
}
try (InputStream is = response.getEntity().getContent();
OutputStream os = new FileOutputStream(f)) {
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.flush();
}
}
}
Aggregations