List of usage examples for org.apache.http.client.methods HttpUriRequest getURI
URI getURI();
From source file:org.sonatype.nexus.repository.httpclient.FilteredHttpClient.java
private static HttpHost determineTarget(final HttpUriRequest request) throws ClientProtocolException { HttpHost target = null;/*from ww w. j a v a 2s . c o m*/ final URI requestURI = request.getURI(); if (requestURI.isAbsolute()) { target = URIUtils.extractHost(requestURI); if (target == null) { throw new ClientProtocolException("URI does not specify a valid host name: " + requestURI); } } return target; }
From source file:wuit.common.crawler.WebSit.Crawler.java
public static String doGetHttp(DSCrawlerUrl pageUrl) { HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 12000); HttpConnectionParams.setSoTimeout(params, 9000); HttpClient httpclient = new DefaultHttpClient(params); String rs = ""; try {/* ww w . j a v a 2s . com*/ HttpGet httpget = new HttpGet(pageUrl.url); // System.out.println("executing request " + pageUrl.url); HttpContext httpContext = new BasicHttpContext(); // httpget.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); httpget.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; .NET CLR 1.1.4322; CIBA; .NET CLR 2.0.50727)"); HttpResponse response = httpclient.execute(httpget, httpContext); HttpUriRequest realRequest = (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST); HttpHost targetHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); pageUrl.url = targetHost.toString() + realRequest.getURI(); int resStatu = response.getStatusLine().getStatusCode();//? if (resStatu == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { rs = EntityUtils.toString(entity, "iso-8859-1"); String in_code = getEncoding(rs); String encode = getHtmlEncode(rs); if (encode.isEmpty()) { httpclient.getConnectionManager().shutdown(); return ""; } else { if (!in_code.toLowerCase().equals("utf-8") && !in_code.toLowerCase().equals(encode.toLowerCase())) { if (!in_code.toLowerCase().equals("iso-8859-1")) rs = new String(rs.getBytes("iso-8859-1"), in_code); if (!encode.toLowerCase().equals(in_code.toLowerCase())) rs = new String(rs.getBytes(in_code), encode); } } try { } catch (RuntimeException ex) { httpget.abort(); throw ex; } finally { // Closing the input stream will trigger connection release // try { instream.close(); } catch (Exception ignore) {} } } } } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); return rs; } }
From source file:org.yamj.api.common.http.HttpClientWrapper.java
protected static HttpHost determineTarget(HttpUriRequest request) throws ClientProtocolException { HttpHost target = null;//from w w w. j av a 2s . c o m URI requestURI = request.getURI(); if (requestURI.isAbsolute()) { target = URIUtils.extractHost(requestURI); if (target == null) { throw new ClientProtocolException("URI does not specify a valid host name: " + requestURI); } } return target; }
From source file:wuit.common.crawler.search.Crawler.java
public static String doGetHttp(String url) { HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 12000); HttpConnectionParams.setSoTimeout(params, 9000); HttpClient httpclient = new DefaultHttpClient(params); String rs = ""; try {//from ww w .j a v a 2 s . co m // System.out.println(url); HttpGet httpget = new HttpGet(url); HttpContext httpContext = new BasicHttpContext(); // httpget.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); httpget.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; .NET CLR 1.1.4322; CIBA; .NET CLR 2.0.50727)"); HttpResponse response = httpclient.execute(httpget, httpContext); HttpUriRequest realRequest = (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST); HttpHost targetHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); url = targetHost.toString() + realRequest.getURI(); int resStatu = response.getStatusLine().getStatusCode();//? if (resStatu == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { rs = EntityUtils.toString(entity, "iso-8859-1"); String in_code = getEncoding(rs); String encode = getHtmlEncode(rs); if (encode.isEmpty()) { httpclient.getConnectionManager().shutdown(); return ""; } else { if (!in_code.toLowerCase().equals("utf-8") && !in_code.toLowerCase().equals(encode.toLowerCase())) { if (!in_code.toLowerCase().equals("iso-8859-1")) rs = new String(rs.getBytes("iso-8859-1"), in_code); if (!encode.toLowerCase().equals(in_code.toLowerCase())) rs = new String(rs.getBytes(in_code), encode); } } try { } catch (RuntimeException ex) { httpget.abort(); throw ex; } finally { // Closing the input stream will trigger connection release // try { instream.close(); } catch (Exception ignore) {} } } } } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); return rs; } }
From source file:com.netflix.http4.NFHttpClient.java
private static HttpHost determineTarget(HttpUriRequest request) throws ClientProtocolException { // A null target may be acceptable if there is a default target. // Otherwise, the null target is detected in the director. HttpHost target = null;/* w ww . jav a 2 s. co m*/ URI requestURI = request.getURI(); if (requestURI.isAbsolute()) { target = URIUtils.extractHost(requestURI); if (target == null) { throw new ClientProtocolException("URI does not specify a valid host name: " + requestURI); } } return target; }
From source file:org.dasein.cloud.azurepack.tests.HttpMethodAsserts.java
private static void assertHttpMethod(HttpUriRequest actualHttpRequest, String expectedHttpMethod, String expectedUrl) {// w ww . jav a 2 s. c o m assertEquals(String.format("Method does not performs a HTTP %s", expectedHttpMethod), expectedHttpMethod, actualHttpRequest.getMethod()); assertEquals(String.format("Method does not performs a HTTP %s at the correct URL", expectedHttpMethod), expectedUrl, actualHttpRequest.getURI().toString()); }
From source file:org.sonatype.nexus.apachehttpclient.page.Page.java
/** * Returns a page for given HTTP request. *//*from w ww . j a va 2 s . c o m*/ public static Page buildPageFor(final PageContext context, final HttpUriRequest httpUriRequest) throws IOException { checkNotNull(context); checkNotNull(httpUriRequest); // TODO: detect redirects LOG.debug("Executing HTTP {} request against {}", httpUriRequest.getMethod(), httpUriRequest.getURI()); final HttpResponse response = context.executeHttpRequest(httpUriRequest); try { if (context.isExpectedResponse(response)) { if (response.getEntity() != null) { return new Page(httpUriRequest, response, Jsoup.parse(response.getEntity().getContent(), null, httpUriRequest.getURI().toString())); } else { // no body return new Page(httpUriRequest, response, null); } } else { throw new UnexpectedPageResponse(httpUriRequest.getURI().toString(), response.getStatusLine()); } } finally { EntityUtils.consumeQuietly(response.getEntity()); } }
From source file:com.mber.client.HTTParty.java
private static Call execute(final HttpUriRequest request) throws IOException { HttpClient client = new DefaultHttpClient(); try {//from ww w.j a va 2s . c o m request.addHeader("REST-API-Version", MBER_VERSION); HttpResponse response = client.execute(request); String body = toString(response.getEntity().getContent()); return new Call(request.getMethod(), request.getURI(), response.getStatusLine().getStatusCode(), body); } finally { client.getConnectionManager().shutdown(); } }
From source file:co.paralleluniverse.fibers.httpclient.FiberHttpClient.java
private static HttpHost determineTarget(final HttpUriRequest request) throws ClientProtocolException { // A null target may be acceptable if there is a default target. // Otherwise, the null target is detected in the director. HttpHost target = null;//w ww . j av a2 s. c o m final URI requestURI = request.getURI(); if (requestURI.isAbsolute()) { target = URIUtils.extractHost(requestURI); if (target == null) throw new ClientProtocolException("URI does not specify a valid host name: " + requestURI); } return target; }
From source file:ch.entwine.weblounge.test.util.TestSiteUtils.java
/** * Test for the correct response when etag header is set. * /*w ww . j a va2s . com*/ * @param request * the http request * @param eTagValue * the expected etag value * @param logger * used to log test output * @param params * the request parameters * @throws Exception * if processing the request fails */ public static void testETagHeader(HttpUriRequest request, String eTagValue, Logger logger, String[][] params) throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); try { request.removeHeaders("If-Modified-Since"); request.setHeader("If-None-Match", eTagValue); logger.info("Sending 'If-None-Match' request to {}", request.getURI()); HttpResponse response = TestUtils.request(httpClient, request, params); assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatusLine().getStatusCode()); assertNull(response.getEntity()); } finally { httpClient.getConnectionManager().shutdown(); } httpClient = new DefaultHttpClient(); try { request.removeHeaders("If-Modified-Since"); request.setHeader("If-None-Match", "\"abcdefghijklmt\""); logger.info("Sending 'If-None-Match' request to {}", request.getURI()); HttpResponse response = TestUtils.request(httpClient, request, params); assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode()); assertNotNull(response.getEntity()); response.getEntity().consumeContent(); } finally { httpClient.getConnectionManager().shutdown(); } }