Java Code Examples for org.apache.http.client.methods.HttpUriRequest#abort()

The following examples show how to use org.apache.http.client.methods.HttpUriRequest#abort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpPrimeConnection.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Override
public boolean connect(Server server, String primeConnectionsURIPath) throws Exception {
    String url = "http://" + server.getHostPort() + primeConnectionsURIPath;
    logger.debug("Trying URL: {}", url);
    HttpUriRequest get = new HttpGet(url);
    HttpResponse response = null;
    try {
        response = client.execute(get);
        if (logger.isDebugEnabled() && response.getStatusLine() != null) {
            logger.debug("Response code:" + response.getStatusLine().getStatusCode());
        }
    } finally {
       get.abort();
    }
    return true;
}
 
Example 2
Source File: HttpUtil.java    From pulsar-manager with Apache License 2.0 5 votes vote down vote up
public static String httpRequest(HttpUriRequest request, Map<String, String> header) {
    CloseableHttpResponse response = null;
    try {
        for (Map.Entry<String, String> entry: header.entrySet()) {
            request.setHeader(entry.getKey(), entry.getValue());
        }
        if (httpClient == null ) {
            initHttpClient();
        }
        response = httpClient.execute(request);

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String strResult = EntityUtils.toString(response.getEntity());
            response.close();
            return strResult;
        } else {
            request.abort();
        }
    } catch (Throwable cause) {
        log.error("http request exception message: {}, http request error stack: {}",
                cause.getMessage(), cause.getCause());
    } finally {
        try{
            if (response != null) {
                response.close();
            }
        }catch (Exception e){
            log.error("Don't handle exception: {}", e.getMessage());
        }

    }

    return null;
}
 
Example 3
Source File: HttpImpl.java    From tephra with MIT License 5 votes vote down vote up
private void execute(HttpUriRequest request, Map<String, String> requestHeaders, Map<String, String> responseHeaders,
                     OutputStream outputStream) {
    if (!validator.isEmpty(requestHeaders))
        requestHeaders.keySet().stream().filter(key -> !key.toLowerCase().equals("content-length"))
                .forEach(key -> request.addHeader(key, requestHeaders.get(key)));
    request.addHeader("time-hash", numeric.toString(timeHash.generate(), "0"));
    try (CloseableHttpResponse response = HttpClients.custom().setConnectionManager(manager).build()
            .execute(request, HttpClientContext.create())) {
        int statusCode = response.getStatusLine().getStatusCode();
        this.statusCode.set(statusCode);
        if (statusCode != 200) {
            request.abort();
            outputStream.close();
            logger.warn(null, "执行HTTP请求[{}:{}]失败[{}]!", request.getMethod(), request.getURI(), statusCode);

            return;
        }

        HttpEntity httpEntity = response.getEntity();
        if (httpEntity == null) {
            request.abort();
            outputStream.close();
            logger.warn(null, "执行HTTP请求[{}:{}]未返回数据!", request.getMethod(), request.getURI());

            return;
        }

        if (responseHeaders != null)
            for (Header header : response.getAllHeaders())
                responseHeaders.put(header.getName(), header.getValue());
        copy(request, httpEntity, outputStream);
    } catch (Throwable throwable) {
        request.abort();
        logger.warn(throwable, "执行HTTP请求时发生异常[{}]!");
    }
}
 
Example 4
Source File: HttpImpl.java    From tephra with MIT License 5 votes vote down vote up
private void copy(HttpUriRequest request, HttpEntity httpEntity, OutputStream outputStream) {
    try (InputStream inputStream = httpEntity.getContent()) {
        io.copy(inputStream, outputStream);
        outputStream.close();
    } catch (IOException e) {
        request.abort();
        logger.warn(null, "输出HTTP执行结果时发生异常[{}]!", e.getMessage());
    }
}
 
Example 5
Source File: Donkey.java    From LiquidDonkey with MIT License 5 votes vote down vote up
void kill() {
    HttpUriRequest local = request.getAndSet(null);

    if (local != null) {
        logger.debug("-- kill() > killing");
        try {
            local.abort();
        } catch (UnsupportedOperationException ex) {
            logger.warn("-- kill() > exception: {}", ex);
        }
    } else {
        logger.debug("-- kill() > already killed");
    }
}
 
Example 6
Source File: SongDBDownloader.java    From computoser with GNU Affero General Public License v3.0 5 votes vote down vote up
private static String getResponseAsString(String urlString, HttpClient client, HttpContext ctx) throws IOException {
    HttpUriRequest req = new HttpGet(urlString);
    InputStream is = client.execute(req, ctx).getEntity().getContent();
    String result = CharStreams.toString(new InputStreamReader(is));
    is.close();
    req.abort();
    return result;
}
 
Example 7
Source File: StreamClientImpl.java    From TVRemoteIME with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void abort(HttpUriRequest request) {
    request.abort();
}
 
Example 8
Source File: StreamClientImpl.java    From DroidDLNA with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void abort(HttpUriRequest request) {
    request.abort();
}
 
Example 9
Source File: AbstractHttpRequestEmitter.java    From gerbil with GNU Affero General Public License v3.0 4 votes vote down vote up
public void interrupt(HttpUriRequest request) throws UnsupportedOperationException {
    request.abort();
}
 
Example 10
Source File: SongDBDownloader.java    From computoser with GNU Affero General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
        HttpClient client = new DefaultHttpClient();

//        HttpHost proxy = new HttpHost("localhost", 8888);
//        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

        HttpContext ctx = new BasicHttpContext();

        HttpUriRequest req = new HttpGet("http://www.hooktheory.com/analysis/view/the-beatles/i-want-to-hold-your-hand");
        client.execute(req, ctx);
        req.abort();

        List<String> urls = getSongUrls("http://www.hooktheory.com/analysis/browseSearch?sQuery=&sOrderBy=views&nResultsPerPage=525&nPage=1", client, ctx);
        List<List<? extends NameValuePair>> paramsList = new ArrayList<>(urls.size());
        for (String songUrl : urls) {
            paramsList.addAll(getSongParams(songUrl, client, ctx));
        }
        int i = 0;
        for (List<? extends NameValuePair> params : paramsList) {

            HttpPost request = new HttpPost("http://www.hooktheory.com/songs/getXML");

            request.setHeader("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1");
            request.setHeader("Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            request.setHeader("Accept-Encoding", "gzip, deflate");
            request.setHeader("Accept-Language", "en,en-us;q=0.7,bg;q=0.3");
            request.setHeader("Content-Type", "application/x-www-form-urlencoded");
            request.setHeader("Origin", "http://www.hooktheory.com");
            request.setHeader("Referer", URLEncoder.encode("http://www.hooktheory.com/swf/DNALive Version 1.0.131.swf", "utf-8"));

            HttpEntity entity = new UrlEncodedFormEntity(params);
            request.setEntity(entity);

            try {
                HttpResponse response = client.execute(request, ctx);
                if (response.getStatusLine().getStatusCode() == 200) {
                    InputStream is = response.getEntity().getContent();
                    String xml = CharStreams.toString(new InputStreamReader(is));
                    is.close();
                    Files.write(xml, new File("c:/tmp/musicdb/" + i + ".xml"), Charset.forName("utf-8"));
                } else {
                    System.out.println(response.getStatusLine());
                    System.out.println(params);
                }
                i++;
                request.abort();
            } catch (Exception ex) {
                System.out.println(params);
                ex.printStackTrace();
            }
        }
    }
 
Example 11
Source File: URIHandle.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
@Override
protected void receiveContent(InputStream content) {
  if (content == null) {
    return;
  }

  try {
    URI uri = get();
    if (uri == null) {
      throw new IllegalStateException("No uri for output");
    }

    HttpUriRequest method = null;
    HttpEntityEnclosingRequestBase receiver = null;
    if (isUsePut()) {
      HttpPut putter = new HttpPut(uri);
      method         = putter;
      receiver       = putter;
    } else {
      HttpPost poster = new HttpPost(uri);
      method          = poster;
      receiver        = poster;
    }

    InputStreamEntity entity = new InputStreamEntity(content, -1);

    receiver.setEntity(entity);

    HttpResponse response = client.execute(method, getContext());
    content.close();

    StatusLine status = response.getStatusLine();

    if (!method.isAborted()) {
      method.abort();
    }

    if (status.getStatusCode() >= 300) {
      throw new MarkLogicIOException("Could not write to "+uri.toString()+": "+status.getReasonPhrase());
    }
  } catch (IOException e) {
    throw new MarkLogicIOException(e);
  }
}