Hi all,

what I am trying to achieve is the following. There´s an internet web site on which there is a form I can fill out and then get results based on my parameters. Now to adapt this whole thing more to my needs and to automatise it I was trying to write a small Java app to do this.

I used the Firefox plugin 'Live Http Headers' to lookup the POST command and parameters that are used on submission of the form. Which looks like this:

Java Code:
http://appsrv.tanzsport.de/dtv-webdbs/turnier/suche.spc

POST /dtv-webdbs/turnier/suche.spc HTTP/1.1
Host: appsrv.tanzsport.de
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv:1.9.1.8) Gecko/20100202 Firefox/3.5.8 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://appsrv.tanzsport.de/dtv-webdbs/turnier/suche.spf
Cookie: DTV_TDB=true; DTV_TDBSearchPrefs_02="rO0ABXNyACZkZS5kdHYud2ViLnR1cm5pZXJkYi5TZWFyY2hQcmVmZXJlbmNlc5+qMA1/rw02AgARTAALY29uanVuY3Rpb250ABNMamF2YS9sYW5nL0Jvb2xlYW47TAAIZGF0dW1CaXN0ABBMamF2YS91dGlsL0RhdGU7TAAIZGF0dW1Wb25xAH4AAkwADmV4dGVuZGVkU2VhcmNocQB+AAFMAANrbmd0AA9MamF2YS91dGlsL1NldDtMAANsdHZxAH4AA0wAD29ubGluZU1lbGR1bmdlbnEAfgABTAAGcGx6QmlzdAASTGphdmEvbGFuZy9TdHJpbmc7TAAGcGx6Vm9ucQB+AARMAAxzYXZlQXNDb29raWVxAH4AAUwABXQxYXJ0cQB+AARMAAh0MWdydXBwZXEAfgAETAAIdDFrbGFzc2VxAH4ABEwABXQyYXJ0cQB+AARMAAh0MmdydXBwZXEAfgAETAAIdDJrbGFzc2VxAH4ABEwACXR1cm5pZXJOcnQAE0xqYXZhL2xhbmcvSW50ZWdlcjt4cHNyABFqYXZhLmxhbmcuQm9vbGVhbs0gcoDVnPruAgABWgAFdmFsdWV4cABzcgAOamF2YS51dGlsLkRhdGVoaoEBS1l0GQMAAHhwdwgAAAEplSxfAHhzcQB+AAl3CAAAASfAiasAeHNxAH4ABwFwcHEAfgAIcHBxAH4ADHQAAlN0dAAGSGdyIElJdAABQ3B0AAVTZW4gSXEAfgAPcA=="; JSESSIONID=4F6478C3ECD8B4B642F1D547844961B2
Content-Type: application/x-www-form-urlencoded
Content-Length: 460
resultOffset=0&resultSize=10&datumVon=3.4.2010&datumBis=3.7.2010&PLZVon=&PLZBis=&turnier1Gruppe=Hgr+II&turnier1Klasse=C&turnier1Turnierart=St&turnier2Gruppe=Sen+I&turnier2Klasse=C&_LTV=on&_LTV=on&_LTV=on&_LTV=on&_LTV=on&_LTV=on&_LTV=on&_LTV=on&_LTV=on&_LTV=on&_LTV=on&_LTV=on&_LTV=on&_LTV=on&_LTV=on&_LTV=on&_kennungen=on&_kennungen=on&conjunction=false&onlineMeldungen=false&_kennungen=on&_kennungen=on&_kennungen=on&_kennungen=on&turnierNr=&saveAsCookie=true

HTTP/1.1 200 OK
Date: Thu, 04 Mar 2010 10:05:52 GMT
Server: Apache/2.2.9 (Debian) mod_jk/1.2.26 PHP/5.2.6-1+lenny3 with Suhosin-Patch mod_ssl/2.2.9 OpenSSL/0.9.8g
Pragma: No-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: no-cache, no-store
Set-Cookie: DTV_TDBSearchPrefs_02="rO0ABXNyACZkZS5kdHYud2ViLnR1cm5pZXJkYi5TZWFyY2hQcmVmZXJlbmNlc5+qMA1/rw02AgARTAALY29uanVuY3Rpb250ABNMamF2YS9sYW5nL0Jvb2xlYW47TAAIZGF0dW1CaXN0ABBMamF2YS91dGlsL0RhdGU7TAAIZGF0dW1Wb25xAH4AAkwADmV4dGVuZGVkU2VhcmNocQB+AAFMAANrbmd0AA9MamF2YS91dGlsL1NldDtMAANsdHZxAH4AA0wAD29ubGluZU1lbGR1bmdlbnEAfgABTAAGcGx6QmlzdAASTGphdmEvbGFuZy9TdHJpbmc7TAAGcGx6Vm9ucQB+AARMAAxzYXZlQXNDb29raWVxAH4AAUwABXQxYXJ0cQB+AARMAAh0MWdydXBwZXEAfgAETAAIdDFrbGFzc2VxAH4ABEwABXQyYXJ0cQB+AARMAAh0MmdydXBwZXEAfgAETAAIdDJrbGFzc2VxAH4ABEwACXR1cm5pZXJOcnQAE0xqYXZhL2xhbmcvSW50ZWdlcjt4cHNyABFqYXZhLmxhbmcuQm9vbGVhbs0gcoDVnPruAgABWgAFdmFsdWV4cABzcgAOamF2YS51dGlsLkRhdGVoaoEBS1l0GQMAAHhwdwgAAAEplSxfAHhzcQB+AAl3CAAAASfAiasAeHNxAH4ABwFwcHEAfgAIcHBxAH4ADHQAAlN0dAAGSGdyIElJdAABQ3B0AAVTZW4gSXEAfgAPcA=="; Version=1; Max-Age=31536000; Expires=Fri, 04-Mar-2011 10:05:52 GMT
Set-Cookie: JSESSIONID=B37294EC2D9376418617EEE88B983561; Path=/dtv-webdbs
Content-Language: de
Vary: Accept-Encoding
Content-Encoding: gzip
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html;charset=UTF-8
By this I found the target URL and the content that is sent to it (or so I thought). So, using Java Restlet, I wrote this little code piece:

Java Code:
public static void main(String[] args) throws IOException {
		Client c = new Client(Protocol.HTTP);

		Form request = new Form();
		request.add("resultOffset", "0");
		request.add("resultSize", "10");
		request.add("datumVon", "5.3.2010");
		request.add("datumBis", "1.8.2010");
		request.add("PLZVon", "");
		request.add("PLZBis", "");
		request.add("turnier1Gruppe", "Hgr II");
		request.add("turnier1Klasse", "C");
		request.add("turnier1Turnierart", "St");
		request.add("turnier2Gruppe", "Sen I");
		request.add("turnier2Klasse", "C");
		request.add("_LTV", "on");
		request.add("_LTV", "on");
		request.add("_LTV", "on");
		request.add("_LTV", "on");
		request.add("_LTV", "on");
		request.add("_LTV", "on");
		request.add("_LTV", "on");
		request.add("_LTV", "on");
		request.add("_LTV", "on");
		request.add("_LTV", "on");
		request.add("_LTV", "on");
		request.add("_LTV", "on");
		request.add("_LTV", "on");
		request.add("_LTV", "on");
		request.add("_LTV", "on");
		request.add("_LTV", "on");
		request.add("_kennungen", "on");
		request.add("_kennungen", "on");
		request.add("conjunction", "false");
		request.add("onlineMeldungen", "false");
		request.add("_kennungen", "on");
		request.add("_kennungen", "on");
		request.add("_kennungen", "on");
		request.add("_kennungen", "on");
		request.add("turnierNr", "");
		request.add("saveAsCookie", "true");
	    
		System.out.println(request.getQueryString());
	   
		String targetURL = "http://appsrv.tanzsport.de/dtv-webdbs/turnier/suche.spc";
	   
		Response r = c.post(targetURL, request.getWebRepresentation(CharacterSet.UTF_8));
		
		if (r.getStatus().isSuccess() && r.isEntityAvailable()) {
			System.out.println("SUCCESS");
		
			InputStream iStream = r.getEntity().getStream(); 
	                
			BufferedReader reader = new BufferedReader( new InputStreamReader(iStream));
	     
			String str = "";
			while( (str = reader.readLine()) != null )
				System.out.println(str);
	       
		} else {
			System.out.println("ERROR: " + r.getStatus().toString());
	    }
	}
As you can see, I used the target URL from the earlier grabbed POST command and the same content. Only when I run the code, I get an all annoying and not very informative Internal Server Error 500.

Does anybody have any experience doing sumething like this? Or has an idea where I could be wrong?

Many thanks in advance.

Oceansoul