Hi,
I am trying to programmatically tur on the log on wire for Http. I tried
using system properties as described in http logging guide.
http://jakarta.apache.org/commons/httpclient/logging.html.
But it seems as it doesn't take effect.
Below are 2 piece of code I wrote with/without logging on and both give
same output.


Code 1: Without logging:

________________________________________________________________________
________________________________________________
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.auth.*;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;

//import org.apache.commons.*;
import java.io.*;

public class HttpClientNew_log {
  
  private static String url = "http://tern:80/VAutomation/basic";;

  public static void main(String[] args) {
    // Create an instance of HttpClient.


    HttpClient client1 = new HttpClient();
    HttpMethod _method1 = new GetMethod(url);
    HttpState _httpState1 = new HttpState();
    HostConfiguration hostConfig1 = new HostConfiguration();
    hostConfig1.setHost("tern",80);
    try {
      // Execute the method.
      int statusCode =
client1.executeMethod(hostConfig1,_method1,_httpState1);

      System.out.println("Status code :" + statusCode);
      if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + _method1.getStatusLine()
+ "StatusCode:" + statusCode);
      }

      // Read the response body.
      byte[] responseBody = _method1.getResponseBody();

      // Deal with the response.
      // Use caution: ensure correct character encoding and is not
binary data
      //      System.out.println("Response Body is " + new
String(responseBody));

      Header[] responseHeaders = _method1.getResponseHeaders();
      //      Header header;
 
System.out.println("----------------------------------------------------
-----------------------------------");
      for( Header header : responseHeaders){
          System.out.println("Headers is " + header.getName() + "and the
value is :" + header.getValue());
      }



    } catch (HttpException e) {
      System.err.println("Fatal protocol violation: " + e.getMessage());
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println("Fatal transport error: " + e.getMessage());
      e.printStackTrace();
    } finally {
      // Release the connection.
      _method1.releaseConnection();
      //      _method2.releaseConnection();
    }  
  }
} 
________________________________________________________________________
__________________________________________________
Output:
Status code :200
May 27, 2007 9:11:48 PM org.apache.commons.httpclient.HttpMethodBase
getResponseBody
WARNING: Going to buffer response body of large or unknown size. Using
getResponseBodyAsStream instead is recommended.
------------------------------------------------------------------------
---------------
Headers is Dateand the value is :Mon, 28 May 2007 04:38:32 GMT
Headers is Serverand the value is :Apache/2.0.40 (Red Hat Linux)
Headers is Connectionand the value is :close
Headers is Transfer-Encodingand the value is :chunked
Headers is Content-Typeand the value is :text/plain; charset=ISO-8859-1




Code 2 : Trying to switch logging programmatically:

________________________________________________________________________
_________________________________________
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.auth.*;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;

//import org.apache.commons.*;
import java.io.*;

public class HttpClientNew_log {
  
  private static String url = "http://tern:80/VAutomation/basic";;

  public static void main(String[] args) {
    // Create an instance of HttpClient.


    HttpClient client1 = new HttpClient();
    HttpMethod _method1 = new GetMethod(url);
    HttpState _httpState1 = new HttpState();
    HostConfiguration hostConfig1 = new HostConfiguration();
    hostConfig1.setHost("tern",80);
System.setProperty("org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime",
"true");
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.
wire", "debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.
commons.httpclient", "debug");
    try {
      // Execute the method.
      int statusCode =
client1.executeMethod(hostConfig1,_method1,_httpState1);

      System.out.println("Status code :" + statusCode);
      if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + _method1.getStatusLine()
+ "StatusCode:" + statusCode);
      }

      // Read the response body.
      byte[] responseBody = _method1.getResponseBody();

      // Deal with the response.
      // Use caution: ensure correct character encoding and is not
binary data
      //      System.out.println("Response Body is " + new
String(responseBody));

      Header[] responseHeaders = _method1.getResponseHeaders();
      //      Header header;
 
System.out.println("----------------------------------------------------
-----------------------------------");
      for( Header header : responseHeaders){
          System.out.println("Headers is " + header.getName() + "and the
value is :" + header.getValue());
      }



    } catch (HttpException e) {
      System.err.println("Fatal protocol violation: " + e.getMessage());
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println("Fatal transport error: " + e.getMessage());
      e.printStackTrace();
    } finally {
      // Release the connection.
      _method1.releaseConnection();
      //      _method2.releaseConnection();
    }  
  }
} 
________________________________________________________________________
______________________________________________

Output:

Status code :200
May 27, 2007 9:13:18 PM org.apache.commons.httpclient.HttpMethodBase
getResponseBody
WARNING: Going to buffer response body of large or unknown size. Using
getResponseBodyAsStream instead is recommended.
------------------------------------------------------------------------
---------------
Headers is Dateand the value is :Mon, 28 May 2007 04:40:02 GMT
Headers is Serverand the value is :Apache/2.0.40 (Red Hat Linux)
Headers is Connectionand the value is :close
Headers is Transfer-Encodingand the value is :chunked
Headers is Content-Typeand the value is :text/plain; charset=ISO-8859-1
________________________________________________________________________
____________________________________________


I am expecting some more log on wire etc. for debugging purposes.
Please do tell me what I missed here. I have constraint that I need to
enable the log programmatically only.


Thanks,
Pankaj Arora

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

Reply via email to