You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by dg...@gmx.at on 2010/10/23 17:39:00 UTC

How to get http headers using httpclient

Hi All,

Is there an example how to get the whole http protocol?
I would need the request, the response header and content.
I tried out to log them but I don’t know how to use the logging api right, and I couldn’t find any example for that.
I want to the information in my application and log all the traffic.
Here is what I tried out yet.
I use:  Eclipse 3.6.1 and httpclient 4.0.3

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


public class TestWireLogging {
	public static void main(String[] args) throws Exception{
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" ,"ERROR");
		String url = "http://google.at";
		System.out.println(url);
		
		HttpClient httpclient= new DefaultHttpClient();
		
		HttpGet httpost = new HttpGet(url);
		
		HttpResponse response = httpclient.execute(httpost);
		HttpEntity entity = response.getEntity();
		
		
		String content = EntityUtils.toString(entity);
		System.out.println(content);
		System.err.println("test");
	}
}

If someone could give me an example,
I would be very grateful.

Thanks,
Flo

-- 
Neu: GMX De-Mail - Einfach wie E-Mail, sicher wie ein Brief!  
Jetzt De-Mail-Adresse reservieren: http://portal.gmx.net/de/go/demail

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: How to get http headers using httpclient

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2010-10-25 at 01:00 +0200, dgzag@gmx.at wrote:
> Hi All,
> I solved the problem myself. I thought the request line would be part of the http-headers. So I didn't find the line in the headers.
> Shame on me, I would had to read the HttpGet and HttpResponse api.
>
>
> The following class prints the whole http request, response header and content.
> 

You might want to have a look at

http://hc.apache.org/httpcomponents-client-ga/logging.html

Oleg




---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: How to get http headers using httpclient

Posted by dg...@gmx.at.
Hi All,
I solved the problem myself. I thought the request line would be part of the http-headers. So I didn't find the line in the headers.
Shame on me, I would had to read the HttpGet and HttpResponse api.

The following class prints the whole http request, response header and content.

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class WireTest2 {
	public static void main(String[] args) throws ClientProtocolException, IOException{
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://google.com");
		System.out.println("request:-------------------");
		System.out.println(httpget.getRequestLine());
		Header headers[] = httpget.getAllHeaders();
		for(Header h:headers){
			System.out.println(h.getName() + ": " + h.getValue());
		}
		HttpResponse response = httpclient.execute(httpget);
		HttpEntity entity = response.getEntity();
		System.out.println("response:-------------------");
		System.out.println(response.getStatusLine());
		headers = response.getAllHeaders();
		for(Header h:headers){
			System.out.println(h.getName() + ": " + h.getValue());
		}
		String content = EntityUtils.toString(entity);
		System.out.println("content:---------------------");
		System.out.println(content);
	}
}

-------- Original-Nachricht --------
> Datum: Sat, 23 Oct 2010 21:29:51 +0200
> Von: <dg...@gmx.at>
> An: "HttpClient User Discussion" <ht...@hc.apache.org>
> Betreff: Re: How to get http headers using httpclient

> Hi,
> thanks for your reply.
> I tried that out but that does not work for me...
> If I set the VM Arguments it throws a ClassNotFoundException.
> If not, the program works without logging.
> 
> I would need logging in my application itself, not in the console. I need
> the data to do the next step and the application should log the traffic  to
> a file or something like that in production mode.
> 
> I tried the following:
> 
> System: Windows 64bit Java 6 Update22  32bit
> Download: Eclipse Classic 3.6.1 32bit Windows
> Download: httpcomponents-client-4.0.3-bin-with-dependencies.zip from
> http://hc.apache.org/downloads.cgi
> 
> Open Eclipse > select workspace > .\workspace
> New JavaProject > WireTestProject > JavaSE-1.6 >Finish
> New package wiretest
> Right klick project > BuildPath > add external archives > Open:
> "httpmime-4.0.3.jar" "apache-mime4j-0.6.jar" "commons-codec-1.3.jar"
> "commons-logging-1.1.1.jar" "httpclient-4.0.3.jar" "httpcore-4.0.1.jar"
> 
> New Class TestWireLogging:
> 
> package wiretest;
> 
> import org.apache.http.HttpEntity;
> import org.apache.http.HttpResponse;
> import org.apache.http.client.HttpClient;
> import org.apache.http.client.methods.HttpGet;
> import org.apache.http.impl.client.DefaultHttpClient;
> import org.apache.http.util.EntityUtils;
> 
> public class TestWireLogging {
> 	public static void main(String[] args) throws Exception{
> 		String url = "http://google.at";
> 		System.out.println(url);
> 		HttpClient httpclient= new DefaultHttpClient();
> 		HttpGet httpost = new HttpGet(url);
> 		HttpResponse response = httpclient.execute(httpost);
> 		HttpEntity entity = response.getEntity();
> 		String content = EntityUtils.toString(entity);
> 		System.out.println(content);
> 		System.err.println("test");
> 	}
> }
> 
> Run > run configurations > new java Application >
> Main class:  wiretest.TestWireLogging
> VM Arguments:
> 
> org.apache.http.impl.conn.level = FINEST
> org.apache.http.impl.client.level = FINEST
> org.apache.http.client.level = FINEST
> org.apache.http.level = FINEST
> 
> Apply > Run > Result:
> 
> java.lang.NoClassDefFoundError: org/apache/http/impl/conn/level
> Caused by: java.lang.ClassNotFoundException:
> org.apache.http.impl.conn.level
> 	at java.net.URLClassLoader$1.run(Unknown Source)
> 	at java.security.AccessController.doPrivileged(Native Method)
> 	at java.net.URLClassLoader.findClass(Unknown Source)
> 	at java.lang.ClassLoader.loadClass(Unknown Source)
> 	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
> 	at java.lang.ClassLoader.loadClass(Unknown Source)
> Exception in thread "main"
> 
> Thanks,
> Flo
> 
> -------- Original-Nachricht --------
> > Datum: Sat, 23 Oct 2010 17:51:33 +0100
> > Von: Carl-Gustaf Harroch <ch...@gmail.com>
> > An: HttpClient User Discussion <ht...@hc.apache.org>
> > Betreff: Re: How to get http headers using httpclient
> 
> > Hi Flo,
> > 
> > Instead of polluting your code, why not put it in the run
> > configuration within eclipse:
> > 
> > Run -> Run configuration -> new Java application -> arguments -> VM
> > arguments and paste the following (change accordingly):
> > 
> > org.apache.http.impl.conn.level = FINEST
> > org.apache.http.impl.client.level = FINEST
> > org.apache.http.client.level = FINEST
> > org.apache.http.level = FINEST
> > 
> > You should have everything dumped within the console.
> > 
> > ./Carl
> > 
> > 
> > On 23 October 2010 16:39,  <dg...@gmx.at> wrote:
> > > Hi All,
> > >
> > > Is there an example how to get the whole http protocol?
> > > I would need the request, the response header and content.
> > > I tried out to log them but I don’t know how to use the logging api
> > right, and I couldn’t find any example for that.
> > > I want to the information in my application and log all the traffic.
> > > Here is what I tried out yet.
> > > I use:  Eclipse 3.6.1 and httpclient 4.0.3
> > >
> > > import org.apache.http.HttpEntity;
> > > import org.apache.http.HttpResponse;
> > > import org.apache.http.client.HttpClient;
> > > import org.apache.http.client.methods.HttpGet;
> > > import org.apache.http.impl.client.DefaultHttpClient;
> > > import org.apache.http.util.EntityUtils;
> > >
> > >
> > > public class TestWireLogging {
> > >        public static void main(String[] args) throws Exception{
> > > 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" ,"ERROR");
> > >                String url = "http://google.at";
> > >                System.out.println(url);
> > >
> > >                HttpClient httpclient= new DefaultHttpClient();
> > >
> > >                HttpGet httpost = new HttpGet(url);
> > >
> > >                HttpResponse response =
> > httpclient.execute(httpost);
> > >                HttpEntity entity = response.getEntity();
> > >
> > >
> > >                String content = EntityUtils.toString(entity);
> > >                System.out.println(content);
> > >                System.err.println("test");
> > >        }
> > > }
> > >
> > > If someone could give me an example,
> > > I would be very grateful.
> > >
> > > Thanks,
> > > Flo
> > >
> > > --
> > > Neu: GMX De-Mail - Einfach wie E-Mail, sicher wie ein Brief!
> > > Jetzt De-Mail-Adresse reservieren: http://portal.gmx.net/de/go/demail
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> > >
> > >
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> > 
> 
> -- 
> Neu: GMX De-Mail - Einfach wie E-Mail, sicher wie ein Brief!  
> Jetzt De-Mail-Adresse reservieren: http://portal.gmx.net/de/go/demail
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 

-- 
GMX DSL Doppel-Flat ab 19,99 &euro;/mtl.! Jetzt auch mit 
gratis Notebook-Flat! http://portal.gmx.net/de/go/dsl

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: How to get http headers using httpclient

Posted by Florian Themayer <dg...@gmx.at>.
Hi,
thanks for your reply.
I tried that out but that does not work for me...
If I set the VM Arguments it throws a ClassNotFoundException.
If not, the program works without logging.

I would need logging in my application itself, not in the console. I need the data to do the next step and the application should log the traffic  to a file or something like that in production mode.

I tried the following:

System: Windows 64bit Java 6 Update22  32bit
Download: Eclipse Classic 3.6.1 32bit Windows
Download: httpcomponents-client-4.0.3-bin-with-dependencies.zip from http://hc.apache.org/downloads.cgi

Open Eclipse > select workspace > .\workspace
New JavaProject > WireTestProject > JavaSE-1.6 >Finish
New package wiretest
Right klick project > BuildPath > add external archives > Open: "httpmime-4.0.3.jar" "apache-mime4j-0.6.jar" "commons-codec-1.3.jar" "commons-logging-1.1.1.jar" "httpclient-4.0.3.jar" "httpcore-4.0.1.jar"

New Class TestWireLogging:

package wiretest;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class TestWireLogging {
	public static void main(String[] args) throws Exception{
		String url = "http://google.at";
		System.out.println(url);
		HttpClient httpclient= new DefaultHttpClient();
		HttpGet httpost = new HttpGet(url);
		HttpResponse response = httpclient.execute(httpost);
		HttpEntity entity = response.getEntity();
		String content = EntityUtils.toString(entity);
		System.out.println(content);
		System.err.println("test");
	}
}

Run > run configurations > new java Application >
Main class:  wiretest.TestWireLogging
VM Arguments:

org.apache.http.impl.conn.level = FINEST
org.apache.http.impl.client.level = FINEST
org.apache.http.client.level = FINEST
org.apache.http.level = FINEST

Apply > Run > Result:

java.lang.NoClassDefFoundError: org/apache/http/impl/conn/level
Caused by: java.lang.ClassNotFoundException: org.apache.http.impl.conn.level
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
Exception in thread "main"

Thanks,
Flo

-------- Original-Nachricht --------
> Datum: Sat, 23 Oct 2010 17:51:33 +0100
> Von: Carl-Gustaf Harroch <ch...@gmail.com>
> An: HttpClient User Discussion <ht...@hc.apache.org>
> Betreff: Re: How to get http headers using httpclient

> Hi Flo,
> 
> Instead of polluting your code, why not put it in the run
> configuration within eclipse:
> 
> Run -> Run configuration -> new Java application -> arguments -> VM
> arguments and paste the following (change accordingly):
> 
> org.apache.http.impl.conn.level = FINEST
> org.apache.http.impl.client.level = FINEST
> org.apache.http.client.level = FINEST
> org.apache.http.level = FINEST
> 
> You should have everything dumped within the console.
> 
> ./Carl
> 
> 
> On 23 October 2010 16:39,  <dg...@gmx.at> wrote:
> > Hi All,
> >
> > Is there an example how to get the whole http protocol?
> > I would need the request, the response header and content.
> > I tried out to log them but I don’t know how to use the logging api
> right, and I couldn’t find any example for that.
> > I want to the information in my application and log all the traffic.
> > Here is what I tried out yet.
> > I use:  Eclipse 3.6.1 and httpclient 4.0.3
> >
> > import org.apache.http.HttpEntity;
> > import org.apache.http.HttpResponse;
> > import org.apache.http.client.HttpClient;
> > import org.apache.http.client.methods.HttpGet;
> > import org.apache.http.impl.client.DefaultHttpClient;
> > import org.apache.http.util.EntityUtils;
> >
> >
> > public class TestWireLogging {
> >        public static void main(String[] args) throws Exception{
> > 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" ,"ERROR");
> >                String url = "http://google.at";
> >                System.out.println(url);
> >
> >                HttpClient httpclient= new DefaultHttpClient();
> >
> >                HttpGet httpost = new HttpGet(url);
> >
> >                HttpResponse response =
> httpclient.execute(httpost);
> >                HttpEntity entity = response.getEntity();
> >
> >
> >                String content = EntityUtils.toString(entity);
> >                System.out.println(content);
> >                System.err.println("test");
> >        }
> > }
> >
> > If someone could give me an example,
> > I would be very grateful.
> >
> > Thanks,
> > Flo
> >
> > --
> > Neu: GMX De-Mail - Einfach wie E-Mail, sicher wie ein Brief!
> > Jetzt De-Mail-Adresse reservieren: http://portal.gmx.net/de/go/demail
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 

-- 
Neu: GMX De-Mail - Einfach wie E-Mail, sicher wie ein Brief!  
Jetzt De-Mail-Adresse reservieren: http://portal.gmx.net/de/go/demail

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: How to get http headers using httpclient

Posted by Carl-Gustaf Harroch <ch...@gmail.com>.
Hi Flo,

Instead of polluting your code, why not put it in the run
configuration within eclipse:

Run -> Run configuration -> new Java application -> arguments -> VM
arguments and paste the following (change accordingly):

org.apache.http.impl.conn.level = FINEST
org.apache.http.impl.client.level = FINEST
org.apache.http.client.level = FINEST
org.apache.http.level = FINEST

You should have everything dumped within the console.

./Carl


On 23 October 2010 16:39,  <dg...@gmx.at> wrote:
> Hi All,
>
> Is there an example how to get the whole http protocol?
> I would need the request, the response header and content.
> I tried out to log them but I don’t know how to use the logging api right, and I couldn’t find any example for that.
> I want to the information in my application and log all the traffic.
> Here is what I tried out yet.
> I use:  Eclipse 3.6.1 and httpclient 4.0.3
>
> import org.apache.http.HttpEntity;
> import org.apache.http.HttpResponse;
> import org.apache.http.client.HttpClient;
> import org.apache.http.client.methods.HttpGet;
> import org.apache.http.impl.client.DefaultHttpClient;
> import org.apache.http.util.EntityUtils;
>
>
> public class TestWireLogging {
>        public static void main(String[] args) throws Exception{
> 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" ,"ERROR");
>                String url = "http://google.at";
>                System.out.println(url);
>
>                HttpClient httpclient= new DefaultHttpClient();
>
>                HttpGet httpost = new HttpGet(url);
>
>                HttpResponse response = httpclient.execute(httpost);
>                HttpEntity entity = response.getEntity();
>
>
>                String content = EntityUtils.toString(entity);
>                System.out.println(content);
>                System.err.println("test");
>        }
> }
>
> If someone could give me an example,
> I would be very grateful.
>
> Thanks,
> Flo
>
> --
> Neu: GMX De-Mail - Einfach wie E-Mail, sicher wie ein Brief!
> Jetzt De-Mail-Adresse reservieren: http://portal.gmx.net/de/go/demail
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org