Showing posts with label httpconnection. Show all posts
Showing posts with label httpconnection. Show all posts

Thursday, June 23, 2011

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;
        }
    }
}


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.



Friday, April 8, 2011

Sending a soap message from soap client in java

Sending a soap message requires a server name, like --
http://localhost:13000/myservicename
13000 is port no.
Now just create a connection and write soap message to it.
We are using following imports:
import java.net.*;
import java.io.*;
Now just write the code:
URL u = new URL(server);
      URLConnection uc = u.openConnection();
      HttpURLConnection connection = (HttpURLConnection) uc;
      
      connection.setDoOutput(true);
      connection.setDoInput(true);
      connection.setRequestMethod("POST");
      connection.setRequestProperty("SOAPAction", SOAP_ACTION);
      
      OutputStream out = connection.getOutputStream();
      Writer wout = new OutputStreamWriter(out);
      
      wout.write("<?xml version='1.0'?>\r\n");  
      wout.write("<SOAP-ENV:Envelope ");
      wout.write("xmlns:SOAP-ENV=");
      wout.write(
        "'http://schemas.xmlsoap.org/soap/envelope/' "
      );
      wout.write("xmlns:xsi=");
      wout.write(
        "'http://www.w3.org/2001/XMLSchema-instance'>\r\n"); 
      wout.write("  <SOAP-ENV:Body>\r\n");
      wout.write("    <calculateFibonacci ");
      wout.write(
    "xmlns='http://namespaces.cafeconleche.org/xmljava/ch3/'\r\n"
      ); 
      wout.write("    type='xsi:positiveInteger'>" + input 
       + "</calculateFibonacci>\r\n"); 
      wout.write("  </SOAP-ENV:Body>\r\n"); 
      wout.write("</SOAP-ENV:Envelope>\r\n"); 
      
      wout.flush();
      wout.close();
      
      InputStream in = connection.getInputStream();
      int c;
      while ((c = in.read()) != -1) System.out.write(c);
      in.close();

Chitika