Tagged: apache Toggle Comment Threads | Keyboard Shortcuts

  • Subinkrishna Gopi 11:59 am on July 30, 2010 Permalink |
    Tags: apache, code snippet, commons, cookie, , httpclient, , networking, proxy,   

    HttpClient (Apache commons) code sample 

    I was playing around with Apache commons Http utilities the last day. I used to use the java.net.* APIs to satisfy my HTTP(s) needs.

    Here is a sample code which I wrote which takes a URL as input, sets the basic request parameters (e.g. cookie string) and set the proxy settings along with the user credentials.

    import org.apache.commons.httpclient.Credentials;
    import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
    import org.apache.commons.httpclient.Header;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.UsernamePasswordCredentials;
    import org.apache.commons.httpclient.auth.AuthScope;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.params.HttpMethodParams;
    public class HttpClientTest
        public static void main(String[] args)
            HttpClient client = null;
            GetMethod getMethod = null;
            int responseCode = -1;
            byte[] responseStream = null;
            String urlString = "http://www.facebook.com";
            String cookieString = null;
                // Creating the GetMethod instance
                getMethod = new GetMethod(urlString);
                // Retries to establish a successful connection the specified number
                // of times if the initial attempts are not successful.
                        new DefaultHttpMethodRetryHandler(1, false));
                getMethod.getParams().setParameter("http.socket.timeout", new Integer(5000));
                getMethod.setRequestHeader(new Header("Cookie", "<COOKIE_STRING>"));
                // Creating an HttpClient instance
                client = new HttpClient();
                // Proxy settings: Configures the proxy host, port & user
                // credentials and the scope of the credentials.
                client.getHostConfiguration().setProxy("<HOST>", <PORT>);
                Credentials credentials = new UsernamePasswordCredentials
                    ("<USERNAME>", "<PASSWORD>");
                AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);
                client.getState().setProxyCredentials(scope, credentials);
                // Sets the user-agent for the client instance
                client.getParams().setParameter("http.useragent", "<USER_AGENT>");
                // Sends the GET request and gets the response
                responseCode = client.executeMethod(getMethod);
                responseStream = getMethod.getResponseBody();
                System.out.println("Response Code: " + responseCode);
                System.out.println("Response Body: \n" + new String(responseStream));
            catch (Exception e)
                client = null;

    P.S. Did I mention that this is test code? ๐Ÿ™‚

    Note: I was looking for an API set which does make use of its own Socket level implementation. I don’t think HttpClient got it’s own implementation as it claims to be 100% Java. If you know any API set which performs better than java.net.* APIs please feel free to share it.

  • Subinkrishna Gopi 3:30 pm on March 3, 2009 Permalink |
    Tags: apache, , , felix, , knoplerfish,   

    Part 1 – OSGi: What, Why & How 

    What is OSGi?
    OSGi is a Java-based service platform that can be remotely managed. OSGi has been developed by the OSGi Alliance (formerly known as the Open Services Gateway initiative) is an open standards organization founded in March 1999. The OSGi framework defines an application life cycle management model, a service registry, an Execution environment and Modules.

    Read More: http://www.osgi.org/About/WhatIsOSGi
    Download the specification: http://www.osgi.org/Release4/Download

    OSGi Architecture (Source: Wikipedia)


    • Bundles
      Bundles are normal jar components with extra manifest headers. The bundles are loosely coupled & highly cohesive.
    • Services
      The services layer connects bundles in a dynamic way by offering a publish-find-bind model for plain old Java objects (POJO).
    • Services Registry
      The API for management services.
    • Life-Cycle
      The API for life cycle management (install, start, stop, update, and uninstall bundles).
    • Modules
      The layer that defines encapsulation and declaration of dependencies (how a bundle can import and export code).
    • Security
      The layer that handles the security aspects by limit bundle functionality to pre-defined capabilities.
    • Execution Environment
      Defines what methods and classes are available in a specific platform.

    Why should we use OSGi?
    OSGi offers extreme modularity for the developer as the developers will be developing the modules as Bundles which can be plugged in to the application anytime from anywhere. This also enables the reusability and maintainability of the modules. We can even remove some of the modules for upgrade/bug fixes with out disturbing other modules.

    Read more: http://www.osgi.org/About/WhyOSGi

    How can we develop OSGi based applications?
    We can develop an OSGi based application using any of the OSGi implementations. Three of the most widely used implementations are, Equinox (Eclipse Foundation), Apache Felix & Knopflerfish.


Compose new post
Next post/Next comment
Previous post/Previous comment
Show/Hide comments
Go to top
Go to login
Show/Hide help
shift + esc