Walden,

Here is some client code that I had been playing with (attached).  Its
far from production, but it is an example of using HttpClient.





Walden Mathews wrote:
> 
> Okay thanks (I was *almost* there).
> 
> > -----Original Message-----
> > From: Martin van den Bemt [mailto:[EMAIL PROTECTED]]
> > Sent: Wednesday, July 10, 2002 5:51 PM
> > To: Jakarta Commons Users List
> > Subject: RE: [Httpclient] Samples?
> >
> >
> > Do a cvs checkout of the jakart-commons/httpclient
> > and look in the src/test directory..
> > btw they are all based on junit (of course ;))
> >
> > Mvgr,
> > Martin
> >
> > On Wed, 2002-07-10 at 23:46, Walden Mathews wrote:
> > > That sounds promising.  Where would I find them?  Sorry if it's
> > > obvious, but I browsed around for a while...
> > >
> > > Walden
> > >
> > > > -----Original Message-----
> > > > From: Martin van den Bemt [mailto:[EMAIL PROTECTED]]
> > > > Sent: Wednesday, July 10, 2002 5:38 PM
> > > > To: Jakarta Commons Users List
> > > > Subject: Re: [Httpclient] Samples?
> > > >
> > > >
> > > > Maybe a copy & paste from the testcases can help..
> > > >
> > > > Mvgr,
> > > > Martin
> > > >
> > > > On Wed, 2002-07-10 at 23:33, Walden Mathews wrote:
> > > > > Hi,
> > > > >
> > > > > I need to get a small demo up and running quickly.  Can
> > > > someone point
> > > > > me to some samples?  I promise to read the API docs
> > > > carefully in due time.-)
> > > > >
> > > > > Thanks,
> > > > >
> > > > > Walden Mathews
> > > > > ILX Systems
> > > > > 111 Fulton St. 3rd Floor
> > > > > New York, NY 10038
> > > > > Phone (212) 510-3121
> > > > > Fax (212) 520-3800
> > > > >
> > > > > --
> > > > > To unsubscribe, e-mail:
> > > <mailto:[EMAIL PROTECTED]>
> > > > For additional commands, e-mail:
> > > <mailto:[EMAIL PROTECTED]>
> > > >
> > > >
> > >
> > >
> > >
> > > --
> > > To unsubscribe, e-mail:
> > > <mailto:[EMAIL PROTECTED]>
> > > For additional commands, e-mail:
> > > <mailto:[EMAIL PROTECTED]>
> > >
> > > --
> > > To unsubscribe, e-mail:
> <mailto:[EMAIL PROTECTED]>
> > For additional commands, e-mail:
> <mailto:[EMAIL PROTECTED]>
> >
> >
> 
> --
> To unsubscribe, e-mail:
> <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail:
> <mailto:[EMAIL PROTECTED]>
> 
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class httpuser
{
   static String host = "127.0.0.1";
   static int port = 80;
   static HttpClient httpclient;

   static String className = "httpuser";
   static Log logger = LogFactory.getLog(className);

   public static void main(String[] args)
   {
      httpclient = new HttpClient();
      HttpState httpstate = new HttpState();
      //httpstate.setCredentials("Protected Section", new 
UsernamePasswordCredentials("user0", "user0"));
      httpclient.setState(httpstate);
      httpclient.startSession(host, port);
      httpclient.startSession(host, port, new UsernamePasswordCredentials("user0", 
"user0"));

      GetMethod getmethod0 = new GetMethod("/~jsdever/protected/file0.txt");
      handleHttpRequest(getmethod0);
      handleHttpResponse(getmethod0);

      PostMethod postmethod = new PostMethod("/~jsdever/protected/file1.txt");
      postmethod.addParameter("name", "value1");
      System.out.println(postmethod.getParameter("name"));
      postmethod.addParameter("name", "value2");
      System.out.println(postmethod.getParameter("name"));
      handleHttpRequest(postmethod);
      handleHttpResponse(postmethod);

      try{
         httpclient.endSession();
      }catch (Exception ex){
         logger.error("Exception thrown", ex);
      }
   }


   static void handleHttpRequest(HttpMethod httpmethod)
   {
      String methodName = "handleHttpRequest";

      //check for a bogus argument
      if (httpmethod == null){
         logger.warn("httpmethod argument is null");
         return;
      }
      else if (httpmethod.hasBeenUsed()){
         logger.warn("httpmethod has already been executed");
         return;
      }

      //perform the http method execution
      try{
         httpclient.executeMethod(httpmethod);
      }catch (java.io.IOException ioe){
         logger.error("Exception thrown", ioe);
      }catch (HttpException httpe){
         logger.error("Exception thrown", httpe);
      }

      //log the headers
      if (logger.isDebugEnabled()){
         StringBuffer sb = new StringBuffer();
         sb.append(httpmethod.getName());
         sb.append(' ');
         sb.append(httpmethod.getPath());
         sb.append('?');
         sb.append(httpmethod.getQueryString());
         sb.append('\n');
         Header[] headers = httpmethod.getRequestHeaders();
         sb.append("HTTP Request Headers\n");
         for (int i=0; i<headers.length; i++){
            sb.append(headers[i]);
         }
         logger.debug(sb.toString());
      }
   }
   
     

   static void handleHttpResponse(HttpMethod httpmethod)
   {
      String methodName = "handleHttpResponse";

      //check for a bogus argument
      if (httpmethod == null){
         logger.warn("httpmethod argument is null");
         return;
      }
      else if (!httpmethod.hasBeenUsed()){
         logger.warn("httpmethod has not been executed yet");
         return;
      }

      int statusCode = httpmethod.getStatusCode();
      String statusText = httpmethod.getStatusText();
      logger.debug("Webserver returned status: " + statusCode + " - " + statusText);
      if (statusCode >= 300){
         logger.warn("Error status code returned from webserver");
      }

      //log the headers if required
      if (logger.isDebugEnabled()){
         Header[] headers = httpmethod.getResponseHeaders();
         StringBuffer sb = new StringBuffer();
         sb.append("HTTP Response Headers\n");
         for (int i=0; i<headers.length; i++){
            sb.append(headers[i]);
         }
         logger.debug(sb.toString());
      }

      //deal with the cookies
      Header cookieHeader = httpmethod.getResponseHeader("Set-Cookie");
      if (cookieHeader != null){
         try{
            Cookie[] cookies = Cookie.parse(host, port, httpmethod.getPath(), 
cookieHeader);
            logger.debug(cookieHeader);
            for (int i=0; i<cookies.length; i++){
               httpclient.getState().addCookie(cookies[i]);
            }
         }catch (HttpException httpe){
            logger.warn("Failed to parse the cookie header: " + httpe.getMessage());
         }catch (Exception ex){
            logger.warn("Exception thrown when handling cookies", ex);
         }
      }

   }
}

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to