Tagged: http Toggle Comment Threads | Keyboard Shortcuts

  • Subinkrishna Gopi 11:44 am on December 14, 2010 Permalink |
    Tags: crash, http, , putty, ,   

    Today’s read: What to do when your website goes down? 

    Smashin Magazine

    What to do when your website goes down? is an awesome post, trust me. Perhaps one of the most useful one, that can help anyone.

    Have you ever heard a colleague answer the phone like this: “Good afterno… Yes… What? Completely?… When did it go down?… Really, that long?… We’ll look into it right away… Yes, I understand… Of course… Okay, speak to you soon… Bye.” The call may have been followed by some cheesy ’80s rock ballad coming from the speaker phone, interrupted by “Thank you for holding. You are now caller number 126 in the queue.” That’s your boss calling the hosting company’s 24 hour “technical support” line.

    An important website has gone down, and sooner or later, heads will turn to the Web development corner of the office, where you are sitting quietly, minding your own business, regretting that you ever mentioned “Linux” on your CV. You need to take action. Your company needs you. Your client needs you. Here’s what to do.

    Source: Smashing Magazine

    Read the original post

     
  • Subinkrishna Gopi 11:59 am on July 30, 2010 Permalink |
    Tags: , code snippet, commons, cookie, http, 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.

     
  • Subinkrishna Gopi 3:28 pm on March 25, 2010 Permalink |
    Tags: authentication, base 64, encoding, http, , security,   

    Handling HTTP authentication in Java 

    Google Chrome (Ubuntu 9.10)

    Microsoft IE 8 (Windows XP)

    Most of us might have got this pop up when we browse through some websites – especially in intranets. And it’s perfectly fine for the administrator (a.k.a. webmaster) to put such restrictions on users to enhance the information security. Because that’s what a security engineer is expected to do !

    But what if we have to access the resource through our Java code? How to we handle that? I have faced this issue when I was writing a module to crawl through a secured website using HTTPunit.

    We just need to set the Authorization header with the Base64 encoded username and password. Here is a sample code:

    String usernameAndPassword = "myUsername" + ":" + "myPa$$word";
    String encodedString = new sun.misc.BASE64Encoder().encode (usernameAndPassword.getBytes());
    httpConnection.setRequestProperty("Authorization", "Basic " + encodedString);
    

    How Base 64 encoding works?

     
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