use of org.apache.http.client.methods.HttpHead in project PlayerHater by chrisrhoden.
the class PlaylistParser method parsePlaylist.
public static Uri[] parsePlaylist(Uri uri) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpHead(uri.toString()));
Header contentType = response.getEntity().getContentType();
if (contentType != null) {
String mimeType = contentType.getValue().split(";")[0].trim();
for (String plsMimeType : PLS_MIME_TYPES) {
if (plsMimeType.equalsIgnoreCase(mimeType)) {
return parsePls(uri);
}
}
for (String m3uMimeType : M3U_MIME_TYPES) {
if (m3uMimeType.equalsIgnoreCase(mimeType)) {
return parseM3u(uri);
}
}
}
} catch (Exception e) {
}
return new Uri[] { uri };
}
use of org.apache.http.client.methods.HttpHead in project elasticsearch-analysis-ik by medcl.
the class Monitor method run.
/**
* 监控流程:
* ①向词库服务器发送Head请求
* ②从响应中获取Last-Modify、ETags字段值,判断是否变化
* ③如果未变化,休眠1min,返回第①步
* ④如果有变化,重新加载词典
* ⑤休眠1min,返回第①步
*/
public void run() {
//超时设置
RequestConfig rc = RequestConfig.custom().setConnectionRequestTimeout(10 * 1000).setConnectTimeout(10 * 1000).setSocketTimeout(15 * 1000).build();
HttpHead head = new HttpHead(location);
head.setConfig(rc);
//设置请求头
if (last_modified != null) {
head.setHeader("If-Modified-Since", last_modified);
}
if (eTags != null) {
head.setHeader("If-None-Match", eTags);
}
CloseableHttpResponse response = null;
try {
response = httpclient.execute(head);
//返回200 才做操作
if (response.getStatusLine().getStatusCode() == 200) {
if (((response.getLastHeader("Last-Modified") != null) && !response.getLastHeader("Last-Modified").getValue().equalsIgnoreCase(last_modified)) || ((response.getLastHeader("ETag") != null) && !response.getLastHeader("ETag").getValue().equalsIgnoreCase(eTags))) {
// 远程词库有更新,需要重新加载词典,并修改last_modified,eTags
Dictionary.getSingleton().reLoadMainDict();
last_modified = response.getLastHeader("Last-Modified") == null ? null : response.getLastHeader("Last-Modified").getValue();
eTags = response.getLastHeader("ETag") == null ? null : response.getLastHeader("ETag").getValue();
}
} else if (response.getStatusLine().getStatusCode() == 304) {
//没有修改,不做操作
//noop
} else {
logger.info("remote_ext_dict {} return bad code {}", location, response.getStatusLine().getStatusCode());
}
} catch (Exception e) {
logger.error("remote_ext_dict {} error!", e, location);
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
use of org.apache.http.client.methods.HttpHead in project iosched by google.
the class HttpClientStackTest method testCreateHeadRequest.
public void testCreateHeadRequest() throws Exception {
TestRequest.Head request = new TestRequest.Head();
assertEquals(request.getMethod(), Method.HEAD);
HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
assertTrue(httpRequest instanceof HttpHead);
}
use of org.apache.http.client.methods.HttpHead in project crawler4j by yasserg.
the class PageFetcherHtmlOnly method fetchPage.
@Override
public PageFetchResult fetchPage(WebURL webUrl) throws InterruptedException, IOException, PageBiggerThanMaxSizeException {
String toFetchURL = webUrl.getURL();
PageFetchResult fetchResult = new PageFetchResult();
HttpHead head = null;
try {
head = new HttpHead(toFetchURL);
synchronized (mutex) {
long now = new Date().getTime();
if (now - this.lastFetchTime < this.config.getPolitenessDelay()) {
Thread.sleep(this.config.getPolitenessDelay() - (now - this.lastFetchTime));
}
this.lastFetchTime = new Date().getTime();
}
HttpResponse response = httpClient.execute(head);
fetchResult.setEntity(response.getEntity());
fetchResult.setResponseHeaders(response.getAllHeaders());
fetchResult.setFetchedUrl(toFetchURL);
fetchResult.setStatusCode(response.getStatusLine().getStatusCode());
String contentType = response.containsHeader("Content-Type") ? response.getFirstHeader("Content-Type").getValue() : null;
String typeStr = (contentType != null) ? contentType.toLowerCase() : "";
if (typeStr.equals("") || (typeStr.contains("text") && typeStr.contains("html"))) {
return super.fetchPage(webUrl);
} else {
return fetchResult;
}
} finally {
if (head != null) {
head.abort();
}
}
}
use of org.apache.http.client.methods.HttpHead in project ats-framework by Axway.
the class HttpClient method performHttpRequest.
/**
* Performs some supported HTTP request. Currently <i>read only<i> requests
* are supported: GET, HEAD and OPTIONS
*
* @param requestedHostRelativeUrl location/query without host and port like: "/my_dir/res?myParam=1"
* @param httpMethodName any of the currently supported HTTP methods: GET, HEAD and OPTIONS
* @param needResponse whether caller wants to get the contents returned from this request,
* if false - then null is returned
* @return the response content if present and requested by the caller
* @throws FileTransferClientException
*/
public String performHttpRequest(String requestedHostRelativeUrl, String httpMethodName, boolean needResponse) throws FileTransferException {
checkClientInitialized();
final String getUrl = constructGetUrl(requestedHostRelativeUrl);
HttpRequestBase httpMethod = null;
if (!StringUtils.isNullOrEmpty(httpMethodName)) {
httpMethodName = httpMethodName.trim().toUpperCase();
switch(httpMethodName) {
case "GET":
httpMethod = new HttpGet(getUrl);
break;
case "HEAD":
httpMethod = new HttpHead(getUrl);
break;
case "OPTIONS":
httpMethod = new HttpOptions(getUrl);
break;
}
}
if (httpMethod == null) {
throw new IllegalArgumentException("This method supports only GET, HEAD and OPTIONS methods while you have provided '" + httpMethodName + "'");
}
log.info("Performing " + httpMethodName + " request to: " + getUrl);
addRequestHeaders(httpMethod);
return (String) processHttpRequest(httpMethod, needResponse, true);
}
Aggregations