You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2003/02/01 19:46:13 UTC

cvs commit: jakarta-commons/httpclient/src/examples TrivialApp.java

jsdever     2003/02/01 10:46:11

  Modified:    httpclient/src/examples TrivialApp.java
  Log:
  Update to make simpler.
  
  Revision  Changes    Path
  1.8       +30 -39    jakarta-commons/httpclient/src/examples/TrivialApp.java
  
  Index: TrivialApp.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/examples/TrivialApp.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TrivialApp.java	23 Jan 2003 22:47:42 -0000	1.7
  +++ TrivialApp.java	1 Feb 2003 18:46:11 -0000	1.8
  @@ -2,6 +2,7 @@
    * $Header$
    * $Revision$
    * $Date$
  + *
    * ====================================================================
    *
    * The Apache Software License, Version 1.1
  @@ -104,15 +105,6 @@
               System.exit(-1);
           }
   
  -        URL url = null;
  -        try {
  -            url = new URL(args[0]);
  -        } catch (MalformedURLException murle) {
  -            System.out.println("<url> argument '" + url
  -                    + "' is not a valid URL");
  -            System.exit(-2);
  -        }
  -
           Credentials creds = null;
           if (args.length >= 3) {
               creds = new UsernamePasswordCredentials(args[1], args[2]);
  @@ -129,29 +121,24 @@
               client.getState().setCredentials(null, creds);
           }
   
  -        //
  -        HostConfiguration hc = new HostConfiguration();
  -        try {
  -            hc.setHost(new URI(url));
  -        } catch(URIException e) {
  -            throw new RuntimeException(e.toString());
  -        }
  -
  -        //start a session with the webserver
  -        client.setHostConfiguration(hc);
  +        String url = args[0];
  +        HttpMethod method = null;
   
           //create a method object
  -        HttpMethod method = new GetMethod(url.getPath());
  -
  -        //turn follow redirects off
  -        method.setFollowRedirects(false);
  -
  -        //turn strict mode on
  -        method.setStrictMode(false);
  +            method = new GetMethod(url);
  +            method.setFollowRedirects(true);
  +            method.setStrictMode(false);
  +        //} catch (MalformedURLException murle) {
  +        //    System.out.println("<url> argument '" + url
  +        //            + "' is not a valid URL");
  +        //    System.exit(-2);
  +        //}
   
           //execute the method
  +        String responseBody = null;
           try{
               client.executeMethod(method);
  +            responseBody = method.getResponseBodyAsString();
           } catch (HttpException he) {
               System.err.println("Http error connecting to '" + url + "'");
               System.err.println(he.getMessage());
  @@ -161,27 +148,31 @@
               System.exit(-3);
           }
   
  -        //get the request headers
  -        Header[] requestHeaders = method.getRequestHeaders();
  -
  -        //get the response headers
  -        Header[] responseHeaders = method.getResponseHeaders();
  -
  -        //get the response body
  -        byte[] responseBody = method.getResponseBody();
   
           //write out the request headers
  -        System.out.println("*** Request Headers ***");
  +        System.out.println("*** Request ***");
  +        System.out.println("Request Path: " + method.getPath());
  +        System.out.println("Request Path: " + method.getQueryString());
  +        Header[] requestHeaders = method.getRequestHeaders();
           for (int i=0; i<requestHeaders.length; i++){
               System.out.print(requestHeaders[i]);
           }
   
           //write out the response headers
  -        System.out.println("*** Response Headers ***");
  +        System.out.println("*** Response ***");
  +        System.out.println("Status Line: " + method.getStatusLine());
  +        Header[] responseHeaders = method.getResponseHeaders();
           for (int i=0; i<responseHeaders.length; i++){
               System.out.print(responseHeaders[i]);
           }
  +
  +        //write out the response body
  +        System.out.println("*** Response Body ***");
  +        System.out.println(responseBody);
  +
  +        //clean up the connection resources
           method.releaseConnection();
  +        method.recycle();
   
           System.exit(0);
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org