Package org.apache.commons.httpclient

Examples of org.apache.commons.httpclient.HttpClient


 

  private void callAndCompare(int jettyPort, int xSocketPort, String path) throws IOException {

    HttpClient httpClient = new HttpClient();

    GetMethod jettyMethod = new GetMethod("http://localhost:" + jettyPort + path);
    int jettyStatusCode = httpClient.executeMethod(jettyMethod);
    String jettyResponse = jettyMethod.getResponseBodyAsString().trim();
    jettyMethod.releaseConnection();


    GetMethod xSocketMethod = new GetMethod("http://localhost:" + xSocketPort + path);
    int xSocketStatusCode = httpClient.executeMethod(xSocketMethod);
    String xSocketResponse = xSocketMethod.getResponseBodyAsString().trim();
    xSocketMethod.releaseConnection();

    Assert.assertEquals(jettyStatusCode, xSocketStatusCode);
    Assert.assertEquals(jettyResponse, xSocketResponse);
View Full Code Here


     * Creates a new connection to the server.
     */
    public static HttpClient createConnection(String hostname, int port, String username, String password, FileSystemOptions fileSystemOptions) throws FileSystemException
    {
        // Create an Http client
        HttpClient client;
        try
        {
            final HttpURL url = new HttpURL(username,
                password,
                hostname,
                port,
                "/");

            // WebdavResource resource = null;
            WebdavResource resource = new WebdavResource()
            {
            };

            if (fileSystemOptions != null)
            {
                String proxyHost = WebdavFileSystemConfigBuilder.getInstance().getProxyHost(fileSystemOptions);
                int proxyPort = WebdavFileSystemConfigBuilder.getInstance().getProxyPort(fileSystemOptions);

                if (proxyHost != null && proxyPort > 0)
                {
                    // resource = new WebdavResource(url, proxyHost, proxyPort);
                    resource.setProxy(proxyHost, proxyPort);
                }
            }

            /*
            if (resource == null)
            {
                resource = new WebdavResource(url);
            }
            resource.setProperties(WebdavResource.NOACTION, 1);
            */
            resource.setHttpURL(url, WebdavResource.NOACTION, 1);

            client = resource.retrieveSessionInstance();
            client.setHttpConnectionManager(new WebdavConnectionManager());
        }
        catch (final IOException e)
        {
            throw new FileSystemException("vfs.provider.webdav/connect.error", hostname, e);
        }
View Full Code Here

        throws FileSystemException
    {
        // Create the file system
        final GenericFileName rootName = (GenericFileName) name;

        HttpClient httpClient = WebdavClientFactory.createConnection(rootName.getHostName(),
            rootName.getPort(),
            rootName.getUserName(),
            rootName.getPassword(),
            fileSystemOptions);
View Full Code Here

    return asrt;
  }

  public static Integer detectMIMEType(String url) {
    try {
      HttpClient httpClient = new HttpClient();
      HeadMethod method = new HeadMethod(url);
      method.getParams().setIntParameter("http.socket.timeout",5000);
      int code = httpClient.executeMethod(method);
      if (code == 200) {
        Header h = method.getResponseHeader("Content-Type");
        if (h != null) {
          HeaderElement[] headElm = h.getElements();
          if(headElm != null & headElm.length > 0){
View Full Code Here

   * @return Reader
   */
  public static HTTPHEADInfo getHeadInfoWithGet(IFeed feed, URL url)
      throws NetworkException {

    HttpClient hClient = new HttpClient();
    HostConfiguration hc = hClient.getHostConfiguration();
    hc = setProxySetttings(hClient, hc);

    GetMethod gMethod = new GetMethod(url.toString());
    gMethod.setRequestHeader("cache-control", "no-cache");
    try {
      int status = hClient.executeMethod(gMethod);
      if (status != HttpStatus.SC_OK) {
        // Check for redirection.
        if (status == HttpStatus.SC_MOVED_PERMANENTLY
            || status == HttpStatus.SC_MOVED_TEMPORARILY
            || status == HttpStatus.SC_SEE_OTHER
            || status == HttpStatus.SC_TEMPORARY_REDIRECT) {
          String redirectLocation;
          Header locationHeader = gMethod
              .getResponseHeader("location");
          if (locationHeader != null) {
            redirectLocation = locationHeader.getValue();
            gMethod = new GetMethod(redirectLocation);
            status = hClient.executeMethod(gMethod);
            if (status != HttpStatus.SC_OK) {
              throw new NetworkException(gMethod.getStatusLine()
                  .getReasonPhrase());
            }
          } else {
            // The response is invalid and did not provide the
            // new
            // location for
            // the resource. Report an error or possibly handle
            // the
            // response
            // like a 404 Not Found error.
          }
        } else {
          if (status == HttpStatus.SC_UNAUTHORIZED) {
            // Retry with password.
            hc = hClient.getHostConfiguration();
            hc = setProxySetttings(hClient, hc);

            hClient.getState()
                .setCredentials(
                    new AuthScope(AuthScope.ANY_HOST,
                        AuthScope.ANY_PORT,
                        AuthScope.ANY_REALM),
                    getCredentials(feed));

            gMethod = new GetMethod(url.toString());
            gMethod.setDoAuthentication(true);
            status = hClient.executeMethod(gMethod);
            if (status != HttpStatus.SC_OK) {
              throw new NetworkException(gMethod.getStatusLine()
                  .getReasonPhrase());
            }
          } else {
View Full Code Here

    // sLog.debug(Messages.getMessage("net.feed.reader", new String[] {
    // url + "", (url != null ? url.getProtocol() : "no-protocol") }));
    // Check if HTTP protocol otherwise assume we have a local file
    if ("http".equals(url.getProtocol())) {

      HttpClient hClient = new HttpClient();
      hClient.getParams().setSoTimeout(NetPropertiesHandler.timeoutValue);
      HostConfiguration hc = hClient.getHostConfiguration();
      hc = setProxySetttings(hClient, hc);

      GetMethod gMethod = new GetMethod(url.toString());
      gMethod.setFollowRedirects(false);
      gMethod.setRequestHeader("cache-control", "no-cache");
      try {
        int status = hClient.executeMethod(gMethod);
        if (status != HttpStatus.SC_OK) {
          // Check for redirection.
          if (status == HttpStatus.SC_MOVED_PERMANENTLY
              || status == HttpStatus.SC_MOVED_TEMPORARILY
              || status == HttpStatus.SC_SEE_OTHER
              || status == HttpStatus.SC_TEMPORARY_REDIRECT) {
            String redirectLocation;
            Header locationHeader = gMethod
                .getResponseHeader("location");
            if (locationHeader != null) {
              redirectLocation = locationHeader.getValue();

              gMethod = new GetMethod(redirectLocation);
              status = hClient.executeMethod(gMethod);
              if (status != HttpStatus.SC_OK) {
                throw new NetworkException(gMethod
                    .getStatusLine().getReasonPhrase());
              }
            } else {
              // The response is invalid and did not provide
              // the
              // new
              // location for
              // the resource. Report an error or possibly
              // handle
              // the
              // response
              // like a 404 Not Found error.
            }
          } else {
            if (status == HttpStatus.SC_UNAUTHORIZED) {
              hc = hClient.getHostConfiguration();
              hc = setProxySetttings(hClient, hc);
              hClient.getState().setCredentials(
                  new AuthScope(AuthScope.ANY_HOST,
                      AuthScope.ANY_PORT,
                      AuthScope.ANY_REALM),
                  getCredentials(pFeed));
              gMethod = new GetMethod(url.toString());
              gMethod.setDoAuthentication(true);
              status = hClient.executeMethod(gMethod);
              if (status != HttpStatus.SC_OK) {
                throw new NetworkException(gMethod
                    .getStatusLine().getReasonPhrase());
              }
            } else {
View Full Code Here

   * @return NetHEADInfo
   */
  public static HTTPHEADInfo getHeadInfo(IFeed feed, URL url)
      throws NetworkException {
    HTTPHEADInfo head = null;
    HttpClient hClient = new HttpClient();
    hClient.getParams().setSoTimeout(25000);
    // hClient.setTimeout(5000); deprecated in 3.0 client
    HostConfiguration hc = hClient.getHostConfiguration();
    hc = setProxySetttings(hClient, hc);
    HeadMethod hMethod = null;
    try {
      hMethod = new HeadMethod(url.toString());
      hMethod.setRequestHeader("cache-control", "no-cache");
      int status = hClient.executeMethod(hMethod);

      if (status != HttpStatus.SC_OK) {
        // Check for redirection.
        if (status == HttpStatus.SC_MOVED_PERMANENTLY
            || status == HttpStatus.SC_MOVED_TEMPORARILY
            || status == HttpStatus.SC_SEE_OTHER
            || status == HttpStatus.SC_TEMPORARY_REDIRECT) {
          String redirectLocation;
          Header locationHeader = hMethod
              .getResponseHeader("location");
          if (locationHeader != null) {
            redirectLocation = locationHeader.getValue();
            hMethod = new HeadMethod(redirectLocation);
            status = hClient.executeMethod(hMethod);
            if (status != HttpStatus.SC_OK) {
              throw new NetworkException(hMethod.getStatusLine()
                  .getReasonPhrase());
            }
          } else {
            // The response is invalid and did not provide the
            // new
            // location for
            // the resource. Report an error or possibly handle
            // the
            // response
            // like a 404 Not Found error.
          }
        } else {
          if (status == HttpStatus.SC_UNAUTHORIZED) {
            // Retry with password.
            hc = hClient.getHostConfiguration();
            try {

              hClient.getState().setCredentials(
                  new AuthScope(AuthScope.ANY_HOST,
                      AuthScope.ANY_PORT,
                      AuthScope.ANY_REALM),
                  getCredentials(feed)

              );
            } catch (Exception e) {
              throw new NetworkException(e.getMessage());
            }
            hMethod = new HeadMethod(url.toString());
            hMethod.setDoAuthentication(true);
            status = hClient.executeMethod(hMethod);
            if (status != HttpStatus.SC_OK) {
              throw new NetworkException(hMethod.getStatusLine()
                  .getReasonPhrase());
            }
          } else {
View Full Code Here

    try {
      boolean append = false;

      if ("http".equals(lUrl.getProtocol())) {
        HttpClient hClient = new HttpClient();
        // CB TODO, get this from core preferences.
        hClient.getParams().setSoTimeout(
            NetPropertiesHandler.timeoutValue);
        HostConfiguration hc = hClient.getHostConfiguration();
        hc = setProxySetttings(hClient, hc);
        gMethod = new GetMethod(lUrl.toExternalForm());

        // Add a byte range header asking for partial content.
        if (pDownload.getAttachment().isIncomplete()) {
          append = true;
          addByteRangeHeader(pDownload.getAttachment(), gMethod);
          gMethod.setFollowRedirects(true);
        } else {
          gMethod.setFollowRedirects(false);
        }
       
        // Need to do redirects manually to get the filename for
        // storage. We follow the redirections until a 200 OK.
        // we break in case of error.

        boolean lContinue = true;
        while (lContinue) {

          int status = hClient.executeMethod(gMethod);
          switch (status) { // Switch the result.
          case HttpStatus.SC_OK: {
            lContinue = false;
          }
            break;
          case HttpStatus.SC_MOVED_PERMANENTLY:
          case HttpStatus.SC_MOVED_TEMPORARILY:
          case HttpStatus.SC_SEE_OTHER:
          case HttpStatus.SC_TEMPORARY_REDIRECT: {
            lRedirect = true;
            // The redirection code fails for
            // localhost, use IP address as a workaround.
            String redirectLocation;
            Header locationHeader = gMethod
                .getResponseHeader("location");

            if (locationHeader != null) {
              redirectLocation = locationHeader.getValue();
              gMethod.setFollowRedirects(true);
              lUrl = new URL(redirectLocation);
            } else {

            }
          }
            break;
          case HttpStatus.SC_PARTIAL_CONTENT:
            // sLog.info("(1) Partial download granted for: "
            // + pUrl.toExternalForm());
            // sLog.info("(2) Start at byte: "
            // + gMethod.getRequestHeader("Range")
            // .getValue());
            lContinue = false;
            break;
          case HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE: {
            // 28-07-2006, Duh? OK let's try again without
            // Range.
            // sLog.warn("(1) Partial download denied for: "
            // + pUrl.toExternalForm());

            Header lRHeader = gMethod
                .getResponseHeader("Content-Range");
            if (lRHeader != null) {
              // sLog.warn("(2) The content-range is: "
              // + lRHeader.getValue());
            }

            // sLog.warn(gMethod.getResponseBodyAsString());

            Header h = gMethod.getRequestHeader("Range");
            gMethod.removeRequestHeader(h);
            append = false;
          }
            break;
          case HttpStatus.SC_UNAUTHORIZED: {
            // Retry with password.
            hc = hClient.getHostConfiguration();
            hc = setProxySetttings(hClient, hc);
            hClient.getState()
                .setCredentials(
                    new AuthScope(AuthScope.ANY_HOST,
                        AuthScope.ANY_PORT,
                        AuthScope.ANY_REALM),
                    getCredentials(pDownload
View Full Code Here

    {
      if (s_log.isDebugEnabled())
      {
        s_log.debug("downloadHttpFile: downloading file (" + destFile.getName() + ") from url: " + url);
      }
      HttpClient client = new HttpClient();
      setupProxy(proxySettings, client, url);

      method = new GetMethod(url.toString());
      method.setFollowRedirects(true);

      resultCode = client.executeMethod(method);
      if (s_log.isDebugEnabled())
      {
        s_log.debug("downloadHttpFile: response code was: " + resultCode);
      }
View Full Code Here

    int conTimeout = DEFAULT_CON_TIMEOUT;
    if (properties != null && properties.containsKey(IConnectionPropertyConstants.CON_TIMEOUT))
      conTimeout = (Integer) properties.get(IConnectionPropertyConstants.CON_TIMEOUT);

    /* Create a new HttpClient */
    HttpClient client = new HttpClient();

    /* Socket Timeout - Max. time to wait for an answer */
    client.getHttpConnectionManager().getParams().setSoTimeout(conTimeout);

    /* Connection Timeout - Max. time to wait for a connection */
    client.getHttpConnectionManager().getParams().setConnectionTimeout(conTimeout);

    return client;
  }
View Full Code Here

TOP

Related Classes of org.apache.commons.httpclient.HttpClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.