All pastes #452544 Raw Edit

Someone

public text v1 · immutable
·published 2007-04-22 10:50 UTC
rendered paste body
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
/**
 * wget "https://www.rdw.nl/Ovi/Paginas/Default.aspx?__VIEWSTATE=dDwtMTMxMzc1ODYyODs7PjiFsXf/84QrAPaX7CubADFIIKqa&txtKenteken=40lxxh&btnZoeken=Zoeken" --server-response --no-check-certificate
 *	--10:45:14--  https://www.rdw.nl/Ovi/Paginas/Default.aspx?__VIEWSTATE=dDwtMTMxMzc1ODYyODs7PjiFsXf/84QrAPaX7CubADFIIKqa&txtKenteken=40lxxh&btnZoeken=Zoeken
 *        => `Default.aspx?__VIEWSTATE=dDwtMTMxMzc1ODYyODs7PjiFsXf%2F84QrAPaX7CubADFIIKqa&txtKenteken=40lxxh&btnZoeken=Zoeken'
 *Resolving www.rdw.nl... 194.151.24.225
 *Connecting to www.rdw.nl|194.151.24.225|:443... connected.
 *WARNING: Certificate verification error for www.rdw.nl: unable to get local issuer certificate
 *HTTP request sent, awaiting response... 
 *HTTP/1.1 302 Found
 *Set-Cookie: ARPT=KIPLYRS*OVI*CLQW; path=/
 *Cache-Control: no-cache
 *Pragma: no-cache
 *Content-Length: 151
 *Content-Type: text/html; charset=utf-8
 *Expires: -1
 *Location: /Ovi/Paginas/Voertuiggegevens.aspx
 *X-Powered-By: ASP.NET
 *X-AspNet-Version: 1.1.4322
 *Set-Cookie: ASP.NET_SessionId=f2tuukaoumqfd455gb4omizu; path=/
 *Date: Sun, 22 Apr 2007 08:48:46 GMT
 *Connection: keep-alive
 *Location: /Ovi/Paginas/Voertuiggegevens.aspx [following]
 *--10:45:14--  https://www.rdw.nl/Ovi/Paginas/Voertuiggegevens.aspx
 *        => `Voertuiggegevens.aspx.2'
 *Reusing existing connection to www.rdw.nl:443.
 *HTTP request sent, awaiting response... 
 *HTTP/1.1 200 OK
 *Cache-Control: no-cache
 *Pragma: no-cache
 *Content-Length: 15583
 *Content-Type: text/html; charset=utf-8
 *Expires: -1
 *X-Powered-By: ASP.NET
 *X-AspNet-Version: 1.1.4322
 *Date: Sun, 22 Apr 2007 08:48:47 GMT
 *Connection: keep-alive
 *Length: 15,583 (15K) [text/html]
 *
 *100%[=================================================================================================================>] 15,583        --.--K/s             
 *
 *10:45:15 (101.46 KB/s) - `Voertuiggegevens.aspx' saved [15583/15583]
 * @author G.V. Tjong A Hung
 *
 */
public class RDWClient { 

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException, NoSuchAlgorithmException{
		// TODO Auto-generated method stub
		/*
		WebLInitializer webLInitializer = new WebLInitializer();
		webLInitializer.execute("bin/nl/decis/mosaic/dal/rdw/rdwExtract.webl");
		//https://www.rdw.nl/Ovi/Paginas/Default.aspx?__VIEWSTATE=dDwtMTMxMzc1ODYyODs7PjiFsXf/84QrAPaX7CubADFIIKqa&txtKenteken=40lxxh&btnZoeken=Zoeken
		*/
		try {
			
			URL urlAddress = new URL("https://www.rdw.nl/Ovi/Paginas/Default.aspx");
			//URL urlAddress = new URL("https://www.rdw.nl/Ovi/Paginas/Default.aspx?__VIEWSTATE=dDwtMTMxMzc1ODYyODs7PjiFsXf/84QrAPaX7CubADFIIKqa&txtKenteken=40lxxh&btnZoeken=Zoeken");
			HttpURLConnection connection = (HttpURLConnection) urlAddress.openConnection();
			
			String key = "dDwtMTMxMzc1ODYyODs7PjiFsXf/84QrAPaX7CubADFIIKqa";
			String kenteken = "40LXXH"; 
			String encoding = "UTF-8";
			String viewstatePost = "__VIEWSTATE="+URLEncoder.encode(key, encoding);
			String kentekenPost = "txtKenteken="+URLEncoder.encode(kenteken, encoding);
			String buttonPost = "btnZoeken=" + URLEncoder.encode("Zoeken", encoding);
			StringBuilder postMessage = new StringBuilder();
			postMessage.append(viewstatePost);
			postMessage.append("&");
			postMessage.append(kentekenPost);
			postMessage.append("&");
			postMessage.append(buttonPost);
			//System.out.println(postMessage.toString());

			connection.setDoOutput(true);
			connection.setRequestMethod("POST");
			//connection.setAllowUserInteraction(false);
			OutputStreamWriter streamwriter = new OutputStreamWriter(connection.getOutputStream());
			System.out.println(postMessage.toString());
			streamwriter.write(postMessage.toString());
			streamwriter.flush();
			streamwriter.close();
			
			//http://www.hccp.org/java-net-cookie-how-to.html
			
			Map<String, List<String>> headerFields = connection.getHeaderFields();
			System.out.println("headers = "+headerFields.toString());
			System.out.println("URL " +connection.getURL());
			List<String> cookies = headerFields.get("Set-Cookie");
			System.out.println(headerFields.get("Location"));
			URL urlAddress2 = new URL("https://www.rdw.nl/Ovi/Paginas/Voertuiggegevens.aspx");
			HttpURLConnection connection2 = (HttpURLConnection) urlAddress2.openConnection();
			for(String cookie : cookies){
				System.out.println("Cookie = "+cookie.toString());
				connection2.setRequestProperty("Cookie", cookie);
			}
			connection2.connect();
			System.out.println("Response: " + connection2.getResponseCode());
			System.out.println(connection2.getResponseMessage());
			System.out.println("URL " +connection2.getURL());
			//System.out.println("Location " +headerFields.get("Location"));
			//connection.setFollowRedirects(true);
			//System.out.println(connection.getContent().toString());
			//System.out.println(connection.getHeaderField("__VIEWSTATE"));
			
			InputStreamReader streamReader = new InputStreamReader(connection2.getInputStream());
        	BufferedReader br = new BufferedReader(streamReader);
        	String line;
        	do{
        		line = br.readLine();
        		if(line!=null)
        			System.out.println(line);
        	}
        	while(line!=null);
    	} catch (MalformedURLException e) {
    		System.out.println("MalformedURLException " +e.toString());
    	}
	}
}