Example usage for org.apache.commons.httpclient HttpMethod setRequestHeader

List of usage examples for org.apache.commons.httpclient HttpMethod setRequestHeader

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod setRequestHeader.

Prototype

public abstract void setRequestHeader(String paramString1, String paramString2);

Source Link

Usage

From source file:org.apache.webdav.lib.WebdavResource.java

/**
 * Generates and adds the "Transaction" header if this method is part of
 * an externally controlled transaction.
 *///from   w w  w  .  ja v  a  2s  .  c  om
protected void generateTransactionHeader(HttpMethod method) {
    if (client == null || method == null)
        return;

    WebdavState state = (WebdavState) client.getState();
    String txHandle = state.getTransactionHandle();
    if (txHandle != null) {
        method.setRequestHeader("Transaction", "<" + txHandle + ">");
    }
}

From source file:org.apache.webdav.lib.WebdavResource.java

/**
 * Generate and add the If header to the specified HTTP method.
 */// ww w  .j av a 2 s.  co  m
protected void generateIfHeader(HttpMethod method) {

    if (client == null)
        return;
    if (method == null)
        return;

    WebdavState state = (WebdavState) client.getState();
    String[] lockTokens = state.getAllLocks(method.getPath());

    if (lockTokens.length == 0)
        return;

    StringBuffer ifHeaderValue = new StringBuffer();

    for (int i = 0; i < lockTokens.length; i++) {
        ifHeaderValue.append("(<").append(lockTokens[i]).append(">) ");
    }

    method.setRequestHeader("If", ifHeaderValue.toString());

}

From source file:org.apache.webdav.lib.WebdavResource.java

/**
 * Add all additionals headers that have been previously registered
 * with addRequestHeader to the method/*from   www.j a  v  a2 s  . c  o  m*/
 */
protected void generateAdditionalHeaders(HttpMethod method) {
    for (Iterator iterator = headers.keySet().iterator(); iterator.hasNext();) {
        String header = (String) iterator.next();
        method.setRequestHeader(header, (String) headers.get(header));
    }
}

From source file:org.apache.wookie.proxy.ProxyClient.java

private String executeMethod(HttpMethod method, Configuration properties)
        throws Exception, AuthenticationException {
    // Execute the method.
    try {//from  w w w.  j ava  2s  . co  m
        HttpClient client = new HttpClient();

        // set the clients proxy values if needed
        ConnectionsPrefsManager.setProxySettings(client, properties);

        if (fUseProxyAuthentication) {
            if (fBase64Auth != null) {
                method.setRequestHeader("Authorization", fBase64Auth);
            } else {
                List<String> authPrefs = new ArrayList<String>(2);
                authPrefs.add(AuthPolicy.DIGEST);
                authPrefs.add(AuthPolicy.BASIC);
                client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
                // send the basic authentication response even before the server gives an unauthorized response
                client.getParams().setAuthenticationPreemptive(true);
                // Pass our credentials to HttpClient
                client.getState().setCredentials(
                        new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                        new UsernamePasswordCredentials(fProxyUsername, fProxyPassword));
            }
        }

        // Add user language to http request in order to notify server of user's language
        Locale locale = Locale.getDefault();

        method.setRequestHeader("Accept-Language", locale.getLanguage()); //$NON-NLS-1$
        method.removeRequestHeader("Content-Type");
        //method.setRequestHeader("Content-Type","application/json");
        //method.setRequestHeader("Referer", "");
        //method.removeRequestHeader("Referer");
        method.setRequestHeader("Accept", "*/*");

        int statusCode = client.executeMethod(method);

        //System.out.println("response="+method.getResponseBodyAsString());
        //System.out.println("method="+method.toString());
        //System.out.println("response="+method.getResponseBodyAsStream());

        if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
            Header hType = method.getResponseHeader("Content-Type");
            if (hType != null) {
                fContentType = hType.getValue();
            }
            // for now we are only expecting Strings
            //return method.getResponseBodyAsString();
            return readFully(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
        } else if (statusCode == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED
                || statusCode == HttpStatus.SC_UNAUTHORIZED) {
            throw new AuthenticationException("Authentication failed:" + method.getStatusLine() + ' '
                    + method.getURI() + ' ' + method.getStatusText());
        } else {
            throw new Exception("Method failed: " + method.getStatusLine() + ' ' + method.getURI() + ' ' //$NON-NLS-1$
                    + method.getStatusText());
        }
    } catch (IOException e) {
        throw e;
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}

From source file:org.apache.wookie.util.gadgets.GadgetUtils.java

/**
 * Call a remote service/*  w  w  w  .  j av a 2 s  . c  o  m*/
 * @param method the method to invoke
 * @return the response from the remote service
 * @throws Exception
 */
private static String executeMethod(HttpMethod method) throws Exception {
    // Execute the method.
    System.out.println("executeMethod() start");
    try {
        HttpClient client = new HttpClient();

        // Add user language to http request in order to notify server of user's language
        Locale locale = Locale.getDefault();

        method.setRequestHeader("Accept-Language", locale.getLanguage()); //$NON-NLS-1$ 
        int statusCode = client.executeMethod(method);
        System.out.println("HTTP client returned status:" + statusCode);
        if (statusCode == HttpStatus.SC_OK) {
            // for now we are only expecting Strings               
            return method.getResponseBodyAsString();
        } else {
            throw new Exception("Method failed: " + method.getStatusLine() + ' ' + method.getURI() + ' ' //$NON-NLS-1$
                    + method.getStatusText() + method.getResponseBodyAsString());
        }
    } catch (IOException e) {
        System.out.println("CaughtIOException");
        throw new Exception("ERROR_CONNECT", e);
    } finally {
        // Release the connection.
        System.out.println("executeMethod() end");
        method.releaseConnection();
    }
}

From source file:org.archive.crawler.fetcher.FetchHTTP.java

/**
 * Configure the HttpMethod setting options and headers.
 *
 * @param curi CrawlURI from which we pull configuration.
 * @param method The Method to configure.
 * @return HostConfiguration copy customized for this CrawlURI
 *//*  ww w  . j  a va  2s .co m*/
protected HostConfiguration configureMethod(CrawlURI curi, HttpMethod method) {
    // Don't auto-follow redirects
    method.setFollowRedirects(false);

    //        // set soTimeout
    //        method.getParams().setSoTimeout(
    //                ((Integer) getUncheckedAttribute(curi, ATTR_SOTIMEOUT_MS))
    //                        .intValue());

    // Set cookie policy.
    method.getParams()
            .setCookiePolicy((((Boolean) getUncheckedAttribute(curi, ATTR_IGNORE_COOKIES)).booleanValue())
                    ? CookiePolicy.IGNORE_COOKIES
                    : CookiePolicy.BROWSER_COMPATIBILITY);

    // Use only HTTP/1.0 (to avoid receiving chunked responses)
    method.getParams().setVersion(HttpVersion.HTTP_1_0);

    CrawlOrder order = getSettingsHandler().getOrder();
    String userAgent = curi.getUserAgent();
    if (userAgent == null) {
        userAgent = order.getUserAgent(curi);
    }
    method.setRequestHeader("User-Agent", userAgent);
    method.setRequestHeader("From", order.getFrom(curi));

    // Set retry handler.
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HeritrixHttpMethodRetryHandler());

    final long maxLength = getMaxLength(curi);
    if (maxLength > 0 && ((Boolean) getUncheckedAttribute(curi, ATTR_SEND_RANGE)).booleanValue()) {
        method.addRequestHeader(RANGE, RANGE_PREFIX.concat(Long.toString(maxLength - 1)));
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_CONNECTION_CLOSE)).booleanValue()) {
        method.addRequestHeader(HEADER_SEND_CONNECTION_CLOSE);
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_REFERER)).booleanValue()
            && (curi.getViaContext() == null || !Link.PREREQ_MISC.equals(curi.getViaContext().toString()))) {
        // RFC2616 says no referer header if referer is https and the url
        // is not
        String via = curi.flattenVia();
        if (via != null && via.length() > 0
                && !(via.startsWith(HTTPS_SCHEME) && curi.getUURI().getScheme().equals(HTTP_SCHEME))) {
            method.setRequestHeader(REFERER, via);
        }
    }

    if (!curi.isPrerequisite()) {
        setConditionalGetHeader(curi, method, ATTR_SEND_IF_MODIFIED_SINCE,
                CoreAttributeConstants.A_LAST_MODIFIED_HEADER, "If-Modified-Since");
        setConditionalGetHeader(curi, method, ATTR_SEND_IF_NONE_MATCH, CoreAttributeConstants.A_ETAG_HEADER,
                "If-None-Match");
    }

    // TODO: What happens if below method adds a header already
    // added above: e.g. Connection, Range, or Referer?
    setAcceptHeaders(curi, method);

    HostConfiguration config = new HostConfiguration(http.getHostConfiguration());
    configureProxy(curi, config);
    configureBindAddress(curi, config);
    return config;
}

From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java

/**
 * Configure the HttpMethod setting options and headers.
 *
 * @param curi CrawlURI from which we pull configuration.
 * @param method The Method to configure.
 * @return HostConfiguration copy customized for this CrawlURI
 *//*from   w w w  .ja  va  2  s.  c o  m*/
protected HostConfiguration configureMethod(CrawlURI curi, HttpMethod method) {
    // Don't auto-follow redirects
    method.setFollowRedirects(false);

    //        // set soTimeout
    //        method.getParams().setSoTimeout(
    //                ((Integer) getUncheckedAttribute(curi, ATTR_SOTIMEOUT_MS))
    //                        .intValue());

    // Set cookie policy.
    method.getParams()
            .setCookiePolicy((((Boolean) getUncheckedAttribute(curi, ATTR_IGNORE_COOKIES)).booleanValue())
                    ? CookiePolicy.IGNORE_COOKIES
                    : CookiePolicy.BROWSER_COMPATIBILITY);

    // Use only HTTP/1.0 (to avoid receiving chunked responses)
    method.getParams().setVersion(HttpVersion.HTTP_1_0);

    CrawlOrder order = getSettingsHandler().getOrder();
    String userAgent = curi.getUserAgent();
    if (userAgent == null) {
        userAgent = order.getUserAgent(curi);
    }
    method.setRequestHeader("User-Agent", userAgent);
    method.setRequestHeader("From", order.getFrom(curi));

    // Set retry handler.
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HeritrixHttpMethodRetryHandler());

    final long maxLength = getMaxLength(curi);
    if (maxLength > 0 && ((Boolean) getUncheckedAttribute(curi, ATTR_SEND_RANGE)).booleanValue()) {
        method.addRequestHeader(RANGE, RANGE_PREFIX.concat(Long.toString(maxLength - 1)));
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_CONNECTION_CLOSE)).booleanValue()) {
        method.addRequestHeader(HEADER_SEND_CONNECTION_CLOSE);
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_REFERER)).booleanValue()
            && (curi.getViaContext() == null || !Link.PREREQ_MISC.equals(curi.getViaContext().toString()))) {
        // RFC2616 says no referer header if referer is https and the url
        // is not
        String via = curi.flattenVia();
        if (via != null && via.length() > 0
                && !(via.startsWith(HTTPS_SCHEME) && curi.getUURI().getScheme().equals(HTTP_SCHEME))) {
            method.setRequestHeader(REFERER, via);
        }
    }

    /*if(!curi.isPrerequisite() && curi.containsKey(URLInfo.MODIFY_TIME) &&
       (Boolean)getUncheckedAttribute(curi, ATTR_SEND_IF_MODIFIED_SINCE)) {
      long modifyTime = curi.getLong(URLInfo.MODIFY_TIME);
      if (modifyTime != 0) {
      Date date = new Date(modifyTime);
      method.setRequestHeader("If-Modified-Since", date.toString());
      logger.debug(curi.getUURI().toString() + " send header modifyTime:" + date.toGMTString());
      }
              
      setConditionalGetHeader(curi, method, ATTR_SEND_IF_MODIFIED_SINCE, 
           CoreAttributeConstants.A_LAST_MODIFIED_HEADER, "If-Modified-Since");
      setConditionalGetHeader(curi, method, ATTR_SEND_IF_NONE_MATCH, 
        CoreAttributeConstants.A_ETAG_HEADER, "If-None-Match");
    }*/

    // TODO: What happens if below method adds a header already
    // added above: e.g. Connection, Range, or Referer?
    setAcceptHeaders(curi, method);

    HttpClient http = getClient();
    HostConfiguration config = new HostConfiguration(http.getHostConfiguration());
    configureProxy(curi, config);
    configureBindAddress(curi, config);
    return config;
}

From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java

private void setAcceptHeaders(CrawlURI curi, HttpMethod get) {
    try {/* w w w. ja va2 s.  c  o m*/
        StringList accept_headers = (StringList) getAttribute(ATTR_ACCEPT_HEADERS, curi);
        if (!accept_headers.isEmpty()) {
            for (ListIterator i = accept_headers.listIterator(); i.hasNext();) {
                String hdr = (String) i.next();
                String[] nvp = hdr.split(": +");
                if (nvp.length == 2) {
                    get.setRequestHeader(nvp[0], nvp[1]);
                } else {
                    logger.warn("Invalid accept header: " + hdr);
                }
            }
        }
    } catch (AttributeNotFoundException e) {
        logger.error(e.getMessage());
    }
}

From source file:org.attribyte.api.http.impl.commons.Commons3Client.java

@Override
public Response send(Request request, RequestOptions options) throws IOException {

    HttpMethod method = null;

    switch (request.getMethod()) {
    case GET:/*from www . ja va2s  .co  m*/
        method = new GetMethod(request.getURI().toString());
        method.setFollowRedirects(options.followRedirects);
        break;
    case DELETE:
        method = new DeleteMethod(request.getURI().toString());
        break;
    case HEAD:
        method = new HeadMethod(request.getURI().toString());
        method.setFollowRedirects(options.followRedirects);
        break;
    case POST:
        method = new PostMethod(request.getURI().toString());
        if (request.getBody() != null) {
            ByteArrayRequestEntity requestEntity = new ByteArrayRequestEntity(request.getBody().toByteArray());
            ((EntityEnclosingMethod) method).setRequestEntity(requestEntity);
        } else {
            PostMethod postMethod = (PostMethod) method;
            Collection<Parameter> parameters = request.getParameters();
            for (Parameter parameter : parameters) {
                String[] values = parameter.getValues();
                for (String value : values) {
                    postMethod.addParameter(parameter.getName(), value);
                }
            }
        }
        break;
    case PUT:
        method = new PutMethod(request.getURI().toString());
        if (request.getBody() != null) {
            ByteArrayRequestEntity requestEntity = new ByteArrayRequestEntity(request.getBody().toByteArray());
            ((EntityEnclosingMethod) method).setRequestEntity(requestEntity);
        }
        break;
    }

    if (userAgent != null && Strings.isNullOrEmpty(request.getHeaderValue("User-Agent"))) {
        method.setRequestHeader("User-Agent", userAgent);
    }

    Collection<Header> headers = request.getHeaders();
    for (Header header : headers) {
        String[] values = header.getValues();
        for (String value : values) {
            method.setRequestHeader(header.getName(), value);
        }
    }

    int responseCode;
    InputStream is = null;

    try {
        responseCode = httpClient.executeMethod(method);
        is = method.getResponseBodyAsStream();
        if (is != null) {
            byte[] body = Request.bodyFromInputStream(is, options.maxResponseBytes);
            ResponseBuilder builder = new ResponseBuilder();
            builder.setStatusCode(responseCode);
            builder.addHeaders(getMap(method.getResponseHeaders()));
            return builder.setBody(body.length != 0 ? body : null).create();
        } else {
            ResponseBuilder builder = new ResponseBuilder();
            builder.setStatusCode(responseCode);
            builder.addHeaders(getMap(method.getResponseHeaders()));
            return builder.create();
        }

    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ioe) {
                //Ignore
            }
        }
        method.releaseConnection();
    }
}

From source file:org.bibsonomy.util.WebUtils.java

/**
 * Reads from a URL and writes the content into a string.
 * //from   w  ww . j  a  v  a2 s.c  o m
 * @param url
 * @param cookie
 * @param postData 
 * @param visitBefore
 * 
 * @return String which holds the page content.
 * 
 * @throws IOException
 */
public static String getContentAsString(final String url, final String cookie, final String postData,
        final String visitBefore) throws IOException {
    if (present(visitBefore)) {
        /*
         * visit URL to get cookies if needed
         */
        client.executeMethod(new GetMethod(visitBefore));
    }

    final HttpMethod method;
    if (present(postData)) {
        /*
         * do a POST request
         */
        final List<NameValuePair> data = new ArrayList<NameValuePair>();

        for (final String s : postData.split(AMP_SIGN)) {
            final String[] p = s.split(EQUAL_SIGN);

            if (p.length != 2) {
                continue;
            }

            data.add(new NameValuePair(p[0], p[1]));
        }

        method = new PostMethod(url);
        ((PostMethod) method).setRequestBody(data.toArray(new NameValuePair[data.size()]));
    } else {
        /*
         * do a GET request
         */
        method = new GetMethod(url);
        method.setFollowRedirects(true);
    }

    /*
     * set cookie
     */
    if (present(cookie)) {
        method.setRequestHeader(COOKIE_HEADER_NAME, cookie);
    }

    /*
     * do request
     */
    final int status = client.executeMethod(method);
    if (status != HttpStatus.SC_OK) {
        throw new IOException(url + " returns: " + status);
    }

    /*
     * FIXME: check content type header to ensure that we only read textual 
     * content (and not a PDF, radio stream or DVD image ...)
     */

    /*
     * collect response
     */
    final String charset = extractCharset(method.getResponseHeader(CONTENT_TYPE_HEADER_NAME).getValue());
    final StringBuilder content = inputStreamToStringBuilder(method.getResponseBodyAsStream(), charset);
    method.releaseConnection();

    final String string = content.toString();
    if (string.length() > 0) {
        return string;
    }

    return null;

}