You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Xiaobo Yang <xi...@gmail.com> on 2005/12/20 00:47:28 UTC

[HttpClient] How to output the http request body?

Hi,

I managed to output the http request head by adding a println statement in
HttpConnection.java (write method). When I tried to output the http request
body, I found it to be a OutputStream object. Could anybody tell me how to
print out the http request body? Also I wonder how to output the http
response for debugging purpose. Thanks in advanced.

Regards,
Xiaobo Yang

Re: [HttpClient] How to output the http request body?

Posted by Andrea Alberto <an...@gmail.com>.
Just use
org.apache.commons.httpclient.methods.PostMethod.getRequestBodyAsString()
and
org.apache.commons.httpclient.methods.PostMethod.getResponseBodyAsString()
or the equivalent method of
org.apache.commons.httpclient.methods.GetMethod

To check how to use them from
http://jakarta.apache.org/commons/httpclient/tutorial.html

    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();

    // Create a method instance.
    GetMethod method = new GetMethod(url);

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
    		new DefaultHttpMethodRetryHandler(3, false));

    try {
      // Execute the method.
      int statusCode = client.executeMethod(method);

      if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + method.getStatusLine());
      }

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




On 12/20/05, Xiaobo Yang <xi...@gmail.com> wrote:
> Hi,
>
> I managed to output the http request head by adding a println statement in
> HttpConnection.java (write method). When I tried to output the http request
> body, I found it to be a OutputStream object. Could anybody tell me how to
> print out the http request body? Also I wonder how to output the http
> response for debugging purpose. Thanks in advanced.
>
> Regards,
> Xiaobo Yang
>
>

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


Re: [HttpClient] How to output the http request body?

Posted by Xiaobo Yang <xi...@gmail.com>.
Thanks for all your kind replies. Now I managed to get the http request
printed out. What I am doing now is to check the webdav request and finally
I found it is related to jakarta slide. In slide, some classes are
overwritten.

On 12/21/05, Oleg Kalnichevski <ol...@apache.org> wrote:
>
> On Wed, Dec 21, 2005 at 05:46:15AM -0800, qiang zhou wrote:
> > But request onle have getInputStream() method. If you want output
> request body, then
> >
> > InputStream in = request.getInputStream();
> > byte[] bytes = new byte[1024];
> > while(in.read(bytes)!=-1) {
> >    //output content.
> > }
> >
> >
> > ----- Original Message ----
> > From: Xiaobo Yang <xi...@gmail.com>
> > To: commons-user@jakarta.apache.org
> > Sent: 2005???12???20??? 7:47:28
> > Subject: [HttpClient] How to output the http request body?
> >
> >
> > Hi,
> >
> > I managed to output the http request head by adding a println statement
> in
> > HttpConnection.java (write method). When I tried to output the http
> request
> > body, I found it to be a OutputStream object. Could anybody tell me how
> to
> > print out the http request body? Also I wonder how to output the http
> > response for debugging purpose. Thanks in advanced.
> >
> > Regards,
> > Xiaobo Yang
> >
>
> Is there any particular reason why you do not want to be using the wire
> logging?
>
> http://jakarta.apache.org/commons/httpclient/logging.html
>
> Oleg
>
>
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>

Re: [HttpClient] How to output the http request body?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, Dec 21, 2005 at 05:46:15AM -0800, qiang zhou wrote:
> But request onle have getInputStream() method. If you want output request body, then
>  
> InputStream in = request.getInputStream();
> byte[] bytes = new byte[1024];
> while(in.read(bytes)!=-1) {
>    //output content.
> }
> 
> 
> ----- Original Message ----
> From: Xiaobo Yang <xi...@gmail.com>
> To: commons-user@jakarta.apache.org
> Sent: 2005???12???20??? 7:47:28
> Subject: [HttpClient] How to output the http request body?
> 
> 
> Hi,
> 
> I managed to output the http request head by adding a println statement in
> HttpConnection.java (write method). When I tried to output the http request
> body, I found it to be a OutputStream object. Could anybody tell me how to
> print out the http request body? Also I wonder how to output the http
> response for debugging purpose. Thanks in advanced.
> 
> Regards,
> Xiaobo Yang
> 

Is there any particular reason why you do not want to be using the wire
logging?

http://jakarta.apache.org/commons/httpclient/logging.html

Oleg


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

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


Re: [HttpClient] How to output the http request body?

Posted by qiang zhou <qi...@yahoo.com>.
But request onle have getInputStream() method. If you want output request body, then
 
InputStream in = request.getInputStream();
byte[] bytes = new byte[1024];
while(in.read(bytes)!=-1) {
   //output content.
}


----- Original Message ----
From: Xiaobo Yang <xi...@gmail.com>
To: commons-user@jakarta.apache.org
Sent: 2005年12月20日 7:47:28
Subject: [HttpClient] How to output the http request body?


Hi,

I managed to output the http request head by adding a println statement in
HttpConnection.java (write method). When I tried to output the http request
body, I found it to be a OutputStream object. Could anybody tell me how to
print out the http request body? Also I wonder how to output the http
response for debugging purpose. Thanks in advanced.

Regards,
Xiaobo Yang

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