Search in sources :

Example 46 with HttpHead

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 };
}
Also used : Header(org.apache.http.Header) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) Uri(android.net.Uri) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpHead(org.apache.http.client.methods.HttpHead)

Example 47 with HttpHead

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);
        }
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) HttpHead(org.apache.http.client.methods.HttpHead) IOException(java.io.IOException)

Example 48 with HttpHead

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);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpHead(org.apache.http.client.methods.HttpHead) HttpHead(org.apache.http.client.methods.HttpHead) TestRequest(com.android.volley.mock.TestRequest)

Example 49 with 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();
        }
    }
}
Also used : PageFetchResult(edu.uci.ics.crawler4j.fetcher.PageFetchResult) HttpResponse(org.apache.http.HttpResponse) HttpHead(org.apache.http.client.methods.HttpHead) Date(java.util.Date)

Example 50 with HttpHead

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);
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpHead(org.apache.http.client.methods.HttpHead)

Aggregations

HttpHead (org.apache.http.client.methods.HttpHead)104 HttpResponse (org.apache.http.HttpResponse)42 HttpGet (org.apache.http.client.methods.HttpGet)30 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)27 IOException (java.io.IOException)26 HttpPut (org.apache.http.client.methods.HttpPut)24 Test (org.junit.Test)24 HttpPost (org.apache.http.client.methods.HttpPost)23 URI (java.net.URI)22 Header (org.apache.http.Header)21 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)19 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)16 HttpDelete (org.apache.http.client.methods.HttpDelete)14 InputStream (java.io.InputStream)13 HttpEntity (org.apache.http.HttpEntity)11 File (java.io.File)9 HttpOptions (org.apache.http.client.methods.HttpOptions)9 StringEntity (org.apache.http.entity.StringEntity)9 HttpPatch (org.apache.http.client.methods.HttpPatch)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6