Updates from July, 2010 Toggle Comment Threads | Keyboard Shortcuts

  • Subinkrishna Gopi 11:59 am on July 30, 2010 Permalink |
    Tags: , 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;
    
            try
            {
                // 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.
                getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                        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)
            {
                e.printStackTrace();
            }
            finally
            {
                getMethod.releaseConnection();
                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.

    Advertisements
     
  • Subinkrishna Gopi 12:16 pm on July 22, 2010 Permalink |
    Tags: apple, , bing, , cell phone, , fastcodesign.com, , , , iloveny.com, market share, , pilothandwriting.com, , search engine, skype, smashin magazine, , today's read,   

    Today’s read: Various ! 

    It’s been a while since I wrote anything here. I was kinda busy with so many things and that made me to stay away from writing for these many days.

    Today I’ve found a few articles in fastcompany.com (via twitter), fastcodesign.com & Smashing Magazine. And I’m really surprised by some of the facts which I found and some facts are really cool.

    1. You Can’t Appreciate How Completely Apple Has Humiliated The Cellphone Industry Until You See These Charts (Article page)
    This really surprised me. This article talks about how Apple rules the cell phone industry in terms of the profit they make even though they sell only 3% of the handsets! Don’t miss the charts in that page. They tell lot of stories.

    2. Google Steals Ideas From Bing, Bing Steals Market Share From Google (Article page)
    This made me happy. I am a big Microsoft fan though I work most on open source ! And I am really happy with the way how Bing works for them.

    3. Create a Font From Your Own Handwriting (Article page)
    This is what the article says !

    If you’re nostalgic for the days of long, handwritten letters, Pilot Pens has unveiled a new Web-based app that’s perfect for you: PilotHandwriting.com will turn your handwriting into your very own digital font, which you can then use in a letter to someone.

    Source: fastcodesign.com

    4. Designing Style Guidelines For Brands And Websites (Article page)
    The article talks more about the branding guidelines, usage of colors, text and fonts with the help of guidelines followed by some of the well known global brands like BlackBerry, I love New York and Skype.

    Have a nice read !

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel