List of usage examples for org.apache.http.protocol ExecutionContext HTTP_REQUEST
String HTTP_REQUEST
To view the source code for org.apache.http.protocol ExecutionContext HTTP_REQUEST.
Click Source Link
From source file:com.examination.afinal.http.RetryHandler.java
@Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { boolean retry = true; Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT); boolean sent = (b != null && b.booleanValue()); if (executionCount > maxRetries) { // ????????5 retry = false; } else if (exceptionBlacklist.contains(exception.getClass())) { // ???????? retry = false;//w ww . j a v a2s . co m } else if (exceptionWhitelist.contains(exception.getClass())) { retry = true; } else if (!sent) { retry = true; } if (retry) { HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); retry = currentReq != null && !"POST".equals(currentReq.getMethod()); } if (retry) { //????? SystemClock.sleep(RETRY_SLEEP_TIME_MILLIS); } else { exception.printStackTrace(); } return retry; }
From source file:com.flyn.net.asynchttp.RetryHandler.java
@Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { boolean retry = true; Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT); boolean sent = (b != null && b); if (executionCount > maxRetries) { // Do not retry if over max retry count retry = false;/*ww w . j a v a2 s.c om*/ } else if (isInList(exceptionBlacklist, exception)) { // immediately cancel retry if the error is blacklisted retry = false; } else if (isInList(exceptionWhitelist, exception)) { // immediately retry if error is whitelisted retry = true; } else if (!sent) { // for most other errors, retry only if request hasn't been fully // sent yet retry = true; } if (retry) { // resend all idempotent requests HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); if (currentReq == null) { return false; } } if (retry) { SystemClock.sleep(retrySleepTimeMS); } else { exception.printStackTrace(); } return retry; }
From source file:org.robam.xutils.http.client.RetryHandler.java
/** * @param exception/*w w w.j a va 2 s .c o m*/ * @param retriedTimes * @param context HTTP,??,ActivityContext * @return */ @Override public boolean retryRequest(IOException exception, int retriedTimes, HttpContext context) { boolean retry = true; if (exception == null || context == null) { return false; } // ?????,?,True Object isReqSent = context.getAttribute(ExecutionContext.HTTP_REQ_SENT); boolean sent = isReqSent == null ? false : (Boolean) isReqSent; // ??? if (retriedTimes > maxRetries) { // ??, retry = false; } else if (exceptionBlackList.contains(exception.getClass())) { // ?BlackList???. retry = false; } else if (exceptionWhiteList.contains(exception.getClass())) { // ?????. retry = true; } else if (!sent) { // ?? retry = true; } if (retry) { try { Object currRequest = context.getAttribute(ExecutionContext.HTTP_REQUEST); if (currRequest != null) { if (currRequest instanceof HttpRequestBase) { // ???GET?retry,?GET,?? HttpRequestBase requestBase = (HttpRequestBase) currRequest; retry = "GET".equals(requestBase.getMethod()); } else if (currRequest instanceof RequestWrapper) { RequestWrapper requestWrapper = (RequestWrapper) currRequest; retry = "GET".equals(requestWrapper.getMethod()); } } else { retry = false; LogUtils.e("retry error, curr request is null"); } } catch (Throwable e) { retry = false; LogUtils.e("retry error", e); } } if (retry) { // sleep a while and retry http request again. SystemClock.sleep(RETRY_SLEEP_INTERVAL); } return retry; }
From source file:com.louding.frame.http.download.RetryHandler.java
@Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { // ??//from www . ja v a2s . c o m boolean retry = true; // ? Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT); boolean sent = (b != null && b.booleanValue()); if (executionCount > maxRetries) { // ???? retry = false; } else if (isInList(exceptionBlacklist, exception)) { // ?????? retry = false; } else if (isInList(exceptionWhitelist, exception)) { // ???? retry = true; } else if (!sent) { // ????? retry = true; } if (retry) { // resend all idempotent requests HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); String requestType = currentReq.getMethod(); retry = !requestType.equals("POST"); } if (retry) { SystemClock.sleep(RETRY_SLEEP_TIME_MILLIS); } else { exception.printStackTrace(); } return retry; }
From source file:com.soulgalore.crawler.core.impl.HTTPClientResponseFetcher.java
public HTMLPageResponse get(CrawlerURL url, boolean getPage, Map<String, String> requestHeaders, boolean followRedirectsToNewDomain) { if (url.isWrongSyntax()) { return new HTMLPageResponse(url, StatusCode.SC_MALFORMED_URI.getCode(), Collections.<String, String>emptyMap(), "", "", 0, "", 0); }//from w w w. j ava 2s .com final HttpGet get = new HttpGet(url.getUri()); for (String key : requestHeaders.keySet()) { get.setHeader(key, requestHeaders.get(key)); } HttpEntity entity = null; final long start = System.currentTimeMillis(); try { HttpContext localContext = new BasicHttpContext(); final HttpResponse resp = httpClient.execute(get, localContext); final long fetchTime = System.currentTimeMillis() - start; // Get the last URL in the redirect chain final HttpHost target = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); final HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST); // Fix when using proxy, relative URI (no proxy used) String newURL; if (req.getURI().toString().startsWith("http")) { newURL = req.getURI().toString(); } else { newURL = target + req.getURI().toString(); } entity = resp.getEntity(); // this is a hack to minimize the amount of memory used // should make this configurable maybe // don't fetch headers for request that don't fetch the body and // response isn't 200 // these headers will not be shown in the results final Map<String, String> headersAndValues = getPage || !StatusCode.isResponseCodeOk(resp.getStatusLine().getStatusCode()) ? getHeaders(resp) : Collections.<String, String>emptyMap(); final String encoding = entity.getContentEncoding() != null ? entity.getContentEncoding().getValue() : ""; final String body = getPage ? getBody(entity, "".equals(encoding) ? "UTF-8" : encoding) : ""; final long size = entity.getContentLength(); // TODO add log when null final String type = (entity.getContentType() != null) ? entity.getContentType().getValue() : ""; final int sc = resp.getStatusLine().getStatusCode(); EntityUtils.consume(entity); // If we want to only collect only URLS that don't redirect to a new domain // This solves the problem with local links like: // http://www.peterhedenskog.com/facebook that redirects to http://www.facebook.com/u/peter.hedenskog // TODO the host check can be done better :) if (!followRedirectsToNewDomain && !newURL.contains(url.getHost())) { return new HTMLPageResponse(url, StatusCode.SC_SERVER_REDIRECT_TO_NEW_DOMAIN.getCode(), Collections.<String, String>emptyMap(), "", "", 0, "", fetchTime); } return new HTMLPageResponse( !url.getUrl().equals(newURL) ? new CrawlerURL(newURL, url.getReferer()) : url, sc, headersAndValues, body, encoding, size, type, fetchTime); } catch (SocketTimeoutException e) { System.err.println(e); return new HTMLPageResponse(url, StatusCode.SC_SERVER_RESPONSE_TIMEOUT.getCode(), Collections.<String, String>emptyMap(), "", "", 0, "", System.currentTimeMillis() - start); } catch (ConnectTimeoutException e) { System.err.println(e); return new HTMLPageResponse(url, StatusCode.SC_SERVER_RESPONSE_TIMEOUT.getCode(), Collections.<String, String>emptyMap(), "", "", 0, "", System.currentTimeMillis() - start); } catch (IOException e) { System.err.println(e); return new HTMLPageResponse(url, StatusCode.SC_SERVER_RESPONSE_UNKNOWN.getCode(), Collections.<String, String>emptyMap(), "", "", 0, "", -1); } finally { get.releaseConnection(); } }
From source file:com.ab.http.RetryHandler.java
/** * ??TODO// w w w.ja va 2s. c o m * @see org.apache.http.client.HttpRequestRetryHandler#retryRequest(java.io.IOException, int, org.apache.http.protocol.HttpContext) * @author: zhaoqp * @date2013-10-22 ?4:23:15 * @version v1.0 */ @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { boolean retry = true; Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT); boolean sent = (b != null && b); if (executionCount > maxRetries) { // Do not retry if over max retry count retry = false; } else if (isInList(exceptionBlacklist, exception)) { // immediately cancel retry if the error is blacklisted retry = false; } else if (isInList(exceptionWhitelist, exception)) { // immediately retry if error is whitelisted retry = true; } else if (!sent) { // for most other errors, retry only if request hasn't been fully sent yet retry = true; } if (retry) { // resend all idempotent requests HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); if (currentReq == null) { return false; } String requestType = currentReq.getMethod(); retry = !requestType.equals("POST"); } if (retry) { SystemClock.sleep(retrySleepTimeMS); } else { exception.printStackTrace(); } return retry; }
From source file:com.mongolduu.android.ng.misc.RedirectHandler.java
public URI getLocationURI(final HttpResponse response, final HttpContext context) throws ProtocolException { if (response == null) { throw new IllegalArgumentException("HTTP response may not be null"); }/*w w w .ja v a2s . c om*/ // get the location header to find out where to redirect to Header locationHeader = response.getFirstHeader("location"); if (locationHeader == null) { // got a redirect response, but no location header throw new ProtocolException( "Received redirect response " + response.getStatusLine() + " but no location header"); } // HERE IS THE MODIFIED LINE OF CODE String location = locationHeader.getValue().replaceAll(" ", "%20"); URI uri; try { uri = new URI(location); } catch (URISyntaxException ex) { throw new ProtocolException("Invalid redirect URI: " + location, ex); } HttpParams params = response.getParams(); // rfc2616 demands the location value be a complete URI // Location = "Location" ":" absoluteURI if (!uri.isAbsolute()) { if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) { throw new ProtocolException("Relative redirect location '" + uri + "' not allowed"); } // Adjust location URI HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (target == null) { throw new IllegalStateException("Target host not available " + "in the HTTP context"); } HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); try { URI requestURI = new URI(request.getRequestLine().getUri()); URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true); uri = URIUtils.resolve(absoluteRequestURI, uri); } catch (URISyntaxException ex) { throw new ProtocolException(ex.getMessage(), ex); } } if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) { RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(REDIRECT_LOCATIONS); if (redirectLocations == null) { redirectLocations = new RedirectLocations(); context.setAttribute(REDIRECT_LOCATIONS, redirectLocations); } URI redirectURI; if (uri.getFragment() != null) { try { HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); redirectURI = URIUtils.rewriteURI(uri, target, true); } catch (URISyntaxException ex) { throw new ProtocolException(ex.getMessage(), ex); } } else { redirectURI = uri; } if (redirectLocations.contains(redirectURI)) { throw new CircularRedirectException("Circular redirect to '" + redirectURI + "'"); } else { redirectLocations.add(redirectURI); } } return uri; }
From source file:lucee.commons.net.http.httpclient.HTTPResponse4Impl.java
public URL getTargetURL() { URL start = getURL();/*from w w w. j a v a 2 s. com*/ HttpUriRequest req = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); URI uri = req.getURI(); String path = uri.getPath(); String query = uri.getQuery(); if (!StringUtil.isEmpty(query)) path += "?" + query; URL _url = start; try { _url = new URL(start.getProtocol(), start.getHost(), start.getPort(), path); } catch (MalformedURLException e) { } return _url; }
From source file:android.core.TestHttpClient.java
public HttpResponse execute(final HttpRequest request, final HttpHost targetHost, final HttpClientConnection conn) throws HttpException, IOException { this.context.setAttribute(ExecutionContext.HTTP_REQUEST, request); this.context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost); this.context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); request.setParams(new DefaultedHttpParams(request.getParams(), this.params)); this.httpexecutor.preProcess(request, this.httpproc, this.context); HttpResponse response = this.httpexecutor.execute(request, conn, this.context); response.setParams(new DefaultedHttpParams(response.getParams(), this.params)); this.httpexecutor.postProcess(response, this.httpproc, this.context); return response; }
From source file:com.adrup.saldo.bank.lf.LfBankManager.java
@Override public Map<AccountHashKey, RemoteAccount> getAccounts(Map<AccountHashKey, RemoteAccount> accounts) throws BankException { Log.d(TAG, "getAccounts()"); HttpClient httpClient = new SaldoHttpClient(mContext); try {// w w w. j a v a 2 s. co m // get login page Log.d(TAG, "getting login page"); HttpContext httpContext = new BasicHttpContext(); String res = HttpHelper.get(httpClient, LOGIN_URL, httpContext); HttpUriRequest currentReq = (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST); HttpHost currentHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); String action = currentHost.toURI() + currentReq.getURI(); Log.e(TAG, "action=" + action); Matcher matcher = Pattern.compile(VIEWSTATE_REGEX).matcher(res); if (!matcher.find()) { Log.e(TAG, "No viewstate match."); Log.d(TAG, res); throw new LfBankException("No viewState match."); } String viewState = matcher.group(1); Log.d(TAG, "viewState= " + viewState); matcher = Pattern.compile(EVENTVALIDATION_REGEX).matcher(res); if (!matcher.find()) { Log.e(TAG, "No eventvalidation match."); Log.d(TAG, res); throw new LfBankException("No eventValidation match."); } String eventValidation = matcher.group(1); Log.d(TAG, "eventValidation= " + eventValidation); // do login post List<NameValuePair> parameters = new ArrayList<NameValuePair>(3); parameters.add(new BasicNameValuePair("__LASTFOCUS", "")); parameters.add(new BasicNameValuePair("__EVENTTARGET", "")); parameters.add(new BasicNameValuePair("__EVENTARGUMENT", "")); parameters.add(new BasicNameValuePair(VIEWSTATE_PARAM, viewState)); parameters.add(new BasicNameValuePair("selMechanism", "PIN-kod")); parameters.add(new BasicNameValuePair(USER_PARAM, mBankLogin.getUsername())); parameters.add(new BasicNameValuePair(PASS_PARAM, mBankLogin.getPassword())); parameters.add(new BasicNameValuePair("btnLogIn.x", "39")); parameters.add(new BasicNameValuePair("btnLogIn.y", "11")); parameters.add(new BasicNameValuePair(EVENTVALIDATION_PARAM, eventValidation)); Log.d(TAG, "logging in..."); res = HttpHelper.post(httpClient, action, parameters); if (res.contains("Felaktig inloggning")) { Log.d(TAG, "auth fail"); throw new AuthenticationException("auth fail"); } Log.d(TAG, "getting accountsUrl"); // token matcher = Pattern.compile(TOKEN_REGEX).matcher(res); if (!matcher.find()) { Log.e(TAG, "No token match."); Log.d(TAG, res); throw new LfBankException("No token match."); } String token = matcher.group(1); Log.d(TAG, "token= " + token); // accountsUrl matcher = Pattern.compile(ACCOUNTS_URL_REGEX).matcher(res); if (!matcher.find()) { Log.e(TAG, "No accountsUrl match."); Log.d(TAG, res); throw new LfBankException("No accountsUrl match."); } String accountsUrl = Html.fromHtml(matcher.group(1)).toString(); accountsUrl += "&_token=" + token; Log.d(TAG, "tokenized accountsUrl= " + accountsUrl); // get accounts page Log.d(TAG, "fetching accounts"); res = HttpHelper.get(httpClient, accountsUrl); matcher = Pattern.compile(ACCOUNTS_REGEX).matcher(res); int remoteId = 1; int count = 0; while (matcher.find()) { count++; int groupCount = matcher.groupCount(); for (int i = 1; i <= groupCount; i++) { Log.d(TAG, i + ":" + matcher.group(i)); } if (groupCount < 2) { throw new BankException("Pattern match issue: groupCount < 2"); } int ordinal = remoteId; String name = Html.fromHtml(matcher.group(1)).toString(); long balance = Long.parseLong(matcher.group(2).replaceAll("\\,|\\.| ", "")) / 100; accounts.put(new AccountHashKey(String.valueOf(remoteId), mBankLogin.getId()), new Account(String.valueOf(remoteId), mBankLogin.getId(), ordinal, name, balance)); remoteId++; } if (count == 0) { Log.d(TAG, "no accounts added"); Log.d(TAG, res); } } catch (IOException e) { Log.e(TAG, e.getMessage(), e); throw new LfBankException(e.getMessage(), e); } catch (HttpException e) { Log.e(TAG, e.getMessage(), e); throw new LfBankException(e.getMessage(), e); } finally { httpClient.getConnectionManager().shutdown(); } return accounts; }