Example usage for org.apache.http.client.methods HttpUriRequest getURI

List of usage examples for org.apache.http.client.methods HttpUriRequest getURI

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpUriRequest getURI.

Prototype

URI getURI();

Source Link

Document

Returns the URI this request uses, such as <code>http://example.org/path/to/file</code>.

Usage

From source file:org.eweb4j.spiderman.plugin.util.PageFetcherImpl.java

/**
 * /*w  w  w. j  av a 2s .c o  m*/
 * @date 2013-1-7 ?11:08:54
 * @param toFetchURL
 * @return
 */
public FetchResult request(FetchRequest req) throws Exception {
    FetchResult fetchResult = new FetchResult();
    HttpUriRequest request = null;
    HttpEntity entity = null;
    String toFetchURL = req.getUrl();
    boolean isPost = false;
    try {
        if (Http.Method.GET.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpGet(toFetchURL);
        else if (Http.Method.POST.equalsIgnoreCase(req.getHttpMethod())) {
            request = new HttpPost(toFetchURL);
            isPost = true;
        } else if (Http.Method.PUT.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpPut(toFetchURL);
        else if (Http.Method.HEAD.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpHead(toFetchURL);
        else if (Http.Method.OPTIONS.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpOptions(toFetchURL);
        else if (Http.Method.DELETE.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpDelete(toFetchURL);
        else
            throw new Exception("Unknown http method name");

        //???,??
        // TODO ?delay?
        synchronized (mutex) {
            //??
            long now = (new Date()).getTime();
            //?Host??
            if (now - lastFetchTime < config.getPolitenessDelay())
                Thread.sleep(config.getPolitenessDelay() - (now - lastFetchTime));
            //????HOST??URL
            lastFetchTime = (new Date()).getTime();
        }

        //GZIP???GZIP?
        request.addHeader("Accept-Encoding", "gzip");
        for (Iterator<Entry<String, String>> it = headers.entrySet().iterator(); it.hasNext();) {
            Entry<String, String> entry = it.next();
            request.addHeader(entry.getKey(), entry.getValue());
        }

        //?
        Header[] headers = request.getAllHeaders();
        for (Header h : headers) {
            Map<String, List<String>> hs = req.getHeaders();
            String key = h.getName();
            List<String> val = hs.get(key);
            if (val == null)
                val = new ArrayList<String>();
            val.add(h.getValue());

            hs.put(key, val);
        }
        req.getCookies().putAll(this.cookies);
        fetchResult.setReq(req);

        HttpEntity reqEntity = null;
        if (Http.Method.POST.equalsIgnoreCase(req.getHttpMethod())
                || Http.Method.PUT.equalsIgnoreCase(req.getHttpMethod())) {
            if (!req.getFiles().isEmpty()) {
                reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                for (Iterator<Entry<String, List<File>>> it = req.getFiles().entrySet().iterator(); it
                        .hasNext();) {
                    Entry<String, List<File>> e = it.next();
                    String paramName = e.getKey();
                    for (File file : e.getValue()) {
                        // For File parameters
                        ((MultipartEntity) reqEntity).addPart(paramName, new FileBody(file));
                    }
                }

                for (Iterator<Entry<String, List<Object>>> it = req.getParams().entrySet().iterator(); it
                        .hasNext();) {
                    Entry<String, List<Object>> e = it.next();
                    String paramName = e.getKey();
                    for (Object paramValue : e.getValue()) {
                        // For usual String parameters
                        ((MultipartEntity) reqEntity).addPart(paramName, new StringBody(
                                String.valueOf(paramValue), "text/plain", Charset.forName("UTF-8")));
                    }
                }
            } else {
                List<NameValuePair> params = new ArrayList<NameValuePair>(req.getParams().size());
                for (Iterator<Entry<String, List<Object>>> it = req.getParams().entrySet().iterator(); it
                        .hasNext();) {
                    Entry<String, List<Object>> e = it.next();
                    String paramName = e.getKey();
                    for (Object paramValue : e.getValue()) {
                        params.add(new BasicNameValuePair(paramName, String.valueOf(paramValue)));
                    }
                }
                reqEntity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
            }

            if (isPost)
                ((HttpPost) request).setEntity(reqEntity);
            else
                ((HttpPut) request).setEntity(reqEntity);
        }

        //??
        HttpResponse response = httpClient.execute(request);
        headers = response.getAllHeaders();
        for (Header h : headers) {
            Map<String, List<String>> hs = fetchResult.getHeaders();
            String key = h.getName();
            List<String> val = hs.get(key);
            if (val == null)
                val = new ArrayList<String>();
            val.add(h.getValue());

            hs.put(key, val);
        }
        //URL
        fetchResult.setFetchedUrl(toFetchURL);
        String uri = request.getURI().toString();
        if (!uri.equals(toFetchURL))
            if (!URLCanonicalizer.getCanonicalURL(uri).equals(toFetchURL))
                fetchResult.setFetchedUrl(uri);

        entity = response.getEntity();
        //???
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            if (statusCode != HttpStatus.SC_NOT_FOUND) {
                Header locationHeader = response.getFirstHeader("Location");
                //301?302?URL??
                if (locationHeader != null && (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                        || statusCode == HttpStatus.SC_MOVED_TEMPORARILY))
                    fetchResult.setMovedToUrl(
                            URLCanonicalizer.getCanonicalURL(locationHeader.getValue(), toFetchURL));
            }
            //???OKURLstatusCode??
            //???
            if (this.site.getSkipStatusCode() != null && this.site.getSkipStatusCode().trim().length() > 0) {
                String[] scs = this.site.getSkipStatusCode().split(",");
                for (String code : scs) {
                    int c = CommonUtil.toInt(code);
                    //????entity
                    if (statusCode == c) {
                        assemPage(fetchResult, entity);
                        break;
                    }
                }
            }
            fetchResult.setStatusCode(statusCode);
            return fetchResult;
        }

        //??
        if (entity != null) {
            fetchResult.setStatusCode(statusCode);
            assemPage(fetchResult, entity);
            return fetchResult;
        }
    } catch (Throwable e) {
        fetchResult.setFetchedUrl(e.toString());
        fetchResult.setStatusCode(Status.INTERNAL_SERVER_ERROR.ordinal());
        return fetchResult;
    } finally {
        try {
            if (entity == null && request != null)
                request.abort();
        } catch (Exception e) {
            throw e;
        }
    }

    fetchResult.setStatusCode(Status.UNSPECIFIED_ERROR.ordinal());
    return fetchResult;
}

From source file:net.tsz.afinal.http.HttpHandler.java

private void makeRequestWithRetries(HttpUriRequest request) throws IOException {
    if (isResume && targetUrl != null) {
        File downloadFile = new File(targetUrl);
        long fileLen = 0;
        if (downloadFile.isFile() && downloadFile.exists()) {
            fileLen = downloadFile.length();
        }/*from  w w  w .  j  a v a  2s. co m*/
        if (fileLen > 0)
            request.setHeader("RANGE", "bytes=" + fileLen + "-");
    }

    boolean retry = true;
    IOException cause = null;
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();//?
    while (retry) {
        try {
            if (isCancelled()) {
                return;
            }

            //POST:?;GET:?
            String requestUrl = request.getURI().toString();
            String requestMethod = request.getMethod();
            ZLogger.d(String.format("<%d><%s> %s", executionCount, requestMethod, requestUrl));

            for (Header header : request.getAllHeaders()) {
                ZLogger.d(String.format("<%s>:<%s>", header.getName(), header.getValue()));
            }

            //??url
            StringBuilder requestParams = new StringBuilder();
            if (requestMethod.equals(POST)) {
                HttpEntityEnclosingRequestBase requestBase = (HttpEntityEnclosingRequestBase) request;
                if (requestBase != null) {
                    HttpEntity entity = requestBase.getEntity();
                    if (entity != null) {
                        InputStream is = entity.getContent();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                        String line = null;
                        try {
                            while ((line = reader.readLine()) != null) {
                                //                            sb.append(line + "/n");
                                //                                Log.d("Nat: makeRequestWithRetries.request.Params.line", line);
                                requestParams.append(line);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                            ZLogger.ef(e.toString());
                        } finally {
                            try {
                                is.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    //                        byte[] paramsArray = EntityUtils.toByteArray(entity);
                    //                        String paramsStr = EntityUtils.toString(entity, "UTF-8");
                    //                        Log.d("Nat: makeRequestWithRetries.request.Params(2)", paramsStr);
                    //                        Log.d("Nat: makeRequestWithRetries.request.Params(3)", EntityUtils.toString(entity));
                }

                //                    Log.d("Nat: makeRequestWithRetries.request.Params(RAW)", requestParams.toString());
            }

            //
            HttpResponse response = client.execute(request, context);
            //                for(Header header : response.getAllHeaders()){
            //                    Log.d("Nat", String.format(" makeRequestWithRetries.respoonse.header <%s><%s>", header.getName(), header.getValue()));
            //                }

            if (isCancelled()) {
                ZLogger.d("Nat: makeRequestWithRetries", "request caneled.");
                return;
            }

            //POST:?;GET:?
            request.getRequestLine();//GET
            ZLogger.d(request.getRequestLine().toString());
            /*if (request.getMethod().equals("post")) {
            HttpParams params = request.getParams();
             params.setParameter(NetFactory.CLIENTSESSION, "");
            request.snetParams(params);
            }*/

            //?????()
            //executionCount < 1,?????
            if (response.containsHeader("needLogin")/* && executionCount < 1*/) {
                //?url?JSSIONID??cookie.
                //?(?),sessionId
                String newSid = MfhLoginService.get().doLogin();
                if (newSid == null) {
                    //TODO
                    //,?
                    //                        Intent intent = new Intent(Constants.ACTION_REDIRECT_TO_LOGIN_H5);
                    //                        BizApplication.getAppContext().sendBroadcast(intent);
                    //                        break;
                } else {
                    String cookie = String.format("%s=%s", FinalHttp.KEY_JSESSIONID, newSid);
                    request.addHeader(FinalHttp.HEADER_SET_COOKIE, cookie);
                    request.addHeader(FinalHttp.HEADER_COOKIE, cookie);
                    request.addHeader(FinalHttp.HEADER_cookie, cookie);
                    // ?
                    //                        MsgBridgeUtil.register();

                    if (requestMethod.equals(POST)) {
                        //                        //EntityJSSIONID
                        String newParams = replaceParam(requestParams.toString(), JSESSIONID, newSid);
                        //                            HttpEntity entity = new StringEntity(newParams);
                        HttpEntity entity = convertToAjaxParams(newParams).getEntity();
                        ((HttpEntityEnclosingRequestBase) request).setEntity(entity);
                    } else if (requestMethod.equals(GET)) {
                        //URLJSSIONID
                        String newRequestUrl = replaceParam(requestUrl, JSESSIONID, newSid);
                        //                            newRequestUrl = replaceParam(newRequestUrl, "lastupdate", "0");
                        URI uri = new URI(newRequestUrl);
                        //                            Log.d("Nat: makeRequestWithRetries.autoLogin.URI", uri.toString());
                        //                                HttpEntityEnclosingRequestBase requestFact = (HttpEntityEnclosingRequestBase)request;
                        //                                requestFact.setURI(uri);
                        ((HttpEntityEnclosingRequestBase) request).setURI(uri);
                    }
                }

                //TODO,?
                retry = (++executionCount <= MAX_RETRY_TIMES)
                        || retryHandler.retryRequest(new IOException("Exception"), executionCount, context);
                //                    ZLogger.d(String.format("%s  %d", retry ? "?" : "??", executionCount));
                if (retry) {
                    continue;
                }
            }

            //?
            handleResponse(response);
            return;
        } catch (UnknownHostException e) {
            ZLogger.e("UnknownHostException:" + e.toString());
            publishProgress(UPDATE_FAILURE, e, "unknownHostExceptioncan't resolve host");
            return;
        } catch (IOException e) {
            ZLogger.e("IOException: " + e.toString());
            cause = e;
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
            publishProgress(UPDATE_FAILURE, e, "unknownHostExceptioncan't resolve host");
        } catch (NullPointerException e) {
            if (e != null) {
                ZLogger.e("NullPointerException: " + e.toString());
                // here's a bug in HttpClient 4.0.x that on some occasions causes
                // DefaultRequestExecutor to throw an NPE, see
                // http://code.google.com/p/android/issues/detail?id=5255
                cause = new IOException("NPE in HttpClient: " + e.getMessage());
                retry = retryHandler.retryRequest(cause, ++executionCount, context);
            } else {
                ZLogger.e("NullPointerException: e is null");
            }
            publishProgress(UPDATE_FAILURE, e, "unknownHostExceptioncan't resolve host");
        } catch (Exception e) {
            ZLogger.e("Exception: " + e.toString());
            cause = new IOException("Unhandled Exception" + e.getMessage());
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
            publishProgress(UPDATE_FAILURE, e, "unknownHostExceptioncan't resolve host");
        }
    }

    // cleaned up to throw IOException
    if (cause != null) {
        throw cause;
    }
    //        else{
    //            //TODO
    //            throw new IOException("");
    //        }
}

From source file:org.apache.http.impl.client.AbstractStatisticsGatheringHttpClient.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  w  w.ja v a 2 s  . 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:org.apache.http.impl.client.AutoRetryHttpClient.java

public HttpResponse execute(final HttpUriRequest request, final HttpContext context) throws IOException {
    final URI uri = request.getURI();
    final HttpHost httpHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    return execute(httpHost, request, context);
}

From source file:org.apache.http.impl.client.cache.CachingHttpAsyncClient.java

@Override
public Future<HttpResponse> execute(final HttpUriRequest request, final HttpContext context,
        final FutureCallback<HttpResponse> callback) {
    final URI uri = request.getURI();
    final HttpHost httpHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    return execute(httpHost, request, context, callback);
}

From source file:org.apache.http.impl.client.cache.CachingHttpClient.java

public HttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException {
    URI uri = request.getURI();
    HttpHost httpHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    return execute(httpHost, request, context);
}

From source file:org.apache.http.impl.client.CloseableHttpClient.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;//  ww  w.jav  a2 s  .co 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:org.apache.http.impl.client.DefaultRequestDirector.java

/**
 * Analyzes a response to check need for a followup.
 *
 * @param roureq    the request and route.
 * @param response  the response to analayze
 * @param context   the context used for the current request execution
 *
 * @return  the followup request and route if there is a followup, or
 *          <code>null</code> if the response should be returned as is
 *
 * @throws HttpException    in case of a problem
 * @throws IOException      in case of an IO problem
 *///from   ww  w.ja  va  2  s .  c  om
protected RoutedRequest handleResponse(final RoutedRequest roureq, final HttpResponse response,
        final HttpContext context) throws HttpException, IOException {

    final HttpRoute route = roureq.getRoute();
    final RequestWrapper request = roureq.getRequest();

    final HttpParams params = request.getParams();

    if (HttpClientParams.isAuthenticating(params)) {
        HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (target == null) {
            target = route.getTargetHost();
        }
        if (target.getPort() < 0) {
            final Scheme scheme = connManager.getSchemeRegistry().getScheme(target);
            target = new HttpHost(target.getHostName(), scheme.getDefaultPort(), target.getSchemeName());
        }

        final boolean targetAuthRequested = this.authenticator.isAuthenticationRequested(target, response,
                this.targetAuthStrategy, targetAuthState, context);

        HttpHost proxy = route.getProxyHost();
        // if proxy is not set use target host instead
        if (proxy == null) {
            proxy = route.getTargetHost();
        }
        final boolean proxyAuthRequested = this.authenticator.isAuthenticationRequested(proxy, response,
                this.proxyAuthStrategy, proxyAuthState, context);

        if (targetAuthRequested) {
            if (this.authenticator.authenticate(target, response, this.targetAuthStrategy, this.targetAuthState,
                    context)) {
                // Re-try the same request via the same route
                return roureq;
            }
        }
        if (proxyAuthRequested) {
            if (this.authenticator.authenticate(proxy, response, this.proxyAuthStrategy, this.proxyAuthState,
                    context)) {
                // Re-try the same request via the same route
                return roureq;
            }
        }
    }

    if (HttpClientParams.isRedirecting(params)
            && this.redirectStrategy.isRedirected(request, response, context)) {

        if (redirectCount >= maxRedirects) {
            throw new RedirectException("Maximum redirects (" + maxRedirects + ") exceeded");
        }
        redirectCount++;

        // Virtual host cannot be used any longer
        virtualHost = null;

        final HttpUriRequest redirect = redirectStrategy.getRedirect(request, response, context);
        final HttpRequest orig = request.getOriginal();
        redirect.setHeaders(orig.getAllHeaders());

        final URI uri = redirect.getURI();
        final HttpHost newTarget = URIUtils.extractHost(uri);
        if (newTarget == null) {
            throw new ProtocolException("Redirect URI does not specify a valid host name: " + uri);
        }

        // Reset auth states if redirecting to another host
        if (!route.getTargetHost().equals(newTarget)) {
            this.log.debug("Resetting target auth state");
            targetAuthState.reset();
            final AuthScheme authScheme = proxyAuthState.getAuthScheme();
            if (authScheme != null && authScheme.isConnectionBased()) {
                this.log.debug("Resetting proxy auth state");
                proxyAuthState.reset();
            }
        }

        final RequestWrapper wrapper = wrapRequest(redirect);
        wrapper.setParams(params);

        final HttpRoute newRoute = determineRoute(newTarget, wrapper, context);
        final RoutedRequest newRequest = new RoutedRequest(wrapper, newRoute);

        if (this.log.isDebugEnabled()) {
            this.log.debug("Redirecting to '" + uri + "' via " + newRoute);
        }

        return newRequest;
    }

    return null;
}

From source file:org.apache.http.impl.client.DefaultRequestDirector.java

/**
 * Analyzes a response to check need for a followup.
 * /*from  ww w .  j a va2s .  com*/
 * @param roureq
 *            the request and route.
 * @param response
 *            the response to analayze
 * @param context
 *            the context used for the current request execution
 * @return the followup request and route if there is a followup, or
 *         {@code null} if the response should be returned as is
 * @throws HttpException
 *             in case of a problem
 * @throws IOException
 *             in case of an IO problem
 */
protected RoutedRequest handleResponse(final RoutedRequest roureq, final HttpResponse response,
        final HttpContext context) throws HttpException, IOException {
    String something = new String();
    String somethingElse = "";
    List<Something> somethingList = null;
    final HttpRoute route = roureq.getRoute();
    final RequestWrapper request = roureq.getRequest();
    final HttpParams params = request.getParams();
    if (HttpClientParams.isAuthenticating(params)) {
        HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (target == null) {
            target = route.getTargetHost();
        }
        if (target.getPort() < 0) {
            final Scheme scheme = connManager.getSchemeRegistry().getScheme(target);
            target = new HttpHost(target.getHostName(), scheme.getDefaultPort(), target.getSchemeName());
        }
        final boolean targetAuthRequested = this.authenticator.isAuthenticationRequested(target, response,
                this.targetAuthStrategy, targetAuthState, context);
        HttpHost proxy = route.getProxyHost();
        if (proxy == null) {
            proxy = route.getTargetHost();
        }
        final boolean proxyAuthRequested = this.authenticator.isAuthenticationRequested(proxy, response,
                this.proxyAuthStrategy, proxyAuthState, context);
        if (targetAuthRequested) {
            if (this.authenticator.authenticate(target, response, this.targetAuthStrategy, this.targetAuthState,
                    context)) {
                return roureq;
            }
        }
        if (proxyAuthRequested) {
            if (this.authenticator.authenticate(proxy, response, this.proxyAuthStrategy, this.proxyAuthState,
                    context)) {
                return roureq;
            }
        }
    }
    if (HttpClientParams.isRedirecting(params)
            && this.redirectStrategy.isRedirected(request, response, context)) {
        if (redirectCount >= maxRedirects) {
            throw new RedirectException("Maximum redirects (" + maxRedirects + ") exceeded");
        }
        redirectCount++;
        virtualHost = null;
        final HttpUriRequest redirect = redirectStrategy.getRedirect(request, response, context);
        final HttpRequest orig = request.getOriginal();
        redirect.setHeaders(orig.getAllHeaders());
        final URI uri = redirect.getURI();
        final HttpHost newTarget = URIUtils.extractHost(uri);
        if (newTarget == null) {
            throw new ProtocolException("Redirect URI does not specify a valid host name: " + uri);
        }
        if (!route.getTargetHost().equals(newTarget)) {
            this.log.debug("Resetting target auth state");
            targetAuthState.reset();
            final AuthScheme authScheme = proxyAuthState.getAuthScheme();
            if (authScheme != null && authScheme.isConnectionBased()) {
                this.log.debug("Resetting proxy auth state");
                proxyAuthState.reset();
            }
        }
        final RequestWrapper wrapper = wrapRequest(redirect);
        wrapper.setParams(params);
        final HttpRoute newRoute = determineRoute(newTarget, wrapper, context);
        final RoutedRequest newRequest = new RoutedRequest(wrapper, newRoute);
        if (this.log.isDebugEnabled()) {
            this.log.debug("Redirecting to '" + uri + "' via " + newRoute);
        }
        return newRequest;
    }
    return null;
}