KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > http > HttpTunnelTest


1 package http;
2
3 import java.net.*;
4 import java.io.*;
5 import java.util.*;
6 import dinamica.*;
7
8 /**
9  * HttpTunnelTest<br>
10  * Calls via HTTP an Action an retrieves a HashMap
11  * containing all published Recordsets.<br>
12  * Creation date: 08/06/2004<br>
13  * (c) 2004 Martin Cordova y Asociados<br>
14  * http://www.martincordova.com<br>
15  * @author Martin Cordova dinamica@martincordova.com
16  */

17 public class HttpTunnelTest
18 {
19
20     public static void main(String JavaDoc[] args) throws Throwable JavaDoc
21     {
22         HashMap map = httpGet("http://localhost/sdk/action/tunnel", true);
23         Recordset rs1 = (Recordset)map.get("query.sql");
24         Recordset rs2 = (Recordset)map.get("query.sql.metadata");
25         System.out.println(rs1);
26         System.out.println(rs2);
27     }
28
29     /**
30      * Retrieve HashMap containing one or more Recordsets
31      * @param url
32      * @param logStdout
33      * @return
34      * @throws Throwable
35      */

36     public static HashMap httpGet(String JavaDoc url, boolean logStdout) throws Throwable JavaDoc
37     {
38
39         //connect via HTTP
40
URL page = new URL(url);
41         HttpURLConnection urlc = (HttpURLConnection)page.openConnection();
42         urlc.setUseCaches(false);
43         
44         if (logStdout)
45         {
46             System.out.println("Waiting for reply...:" + url);
47             System.out.println("Content-type = " + urlc.getContentType());
48             System.out.println("Content-length = " + urlc.getContentLength());
49             System.out.println("Response-code = " + urlc.getResponseCode());
50             System.out.println("Response-message = " + urlc.getResponseMessage());
51         }
52
53         //test for http errors
54
int retCode = urlc.getResponseCode();
55         String JavaDoc retMsg = urlc.getResponseMessage();
56         if (retCode>=400)
57             throw new Throwable JavaDoc("HTTP Error: " + retCode + " - " + retMsg);
58         
59         //read and convert byte stream to HashMap
60
ObjectInputStream in = new ObjectInputStream( new BufferedInputStream(urlc.getInputStream(), urlc.getContentLength()) );
61         HashMap map = (HashMap)in.readObject();
62         in.close();
63
64         if (logStdout)
65         {
66             System.out.println("Document received.");
67         }
68
69         return map;
70
71     }
72     
73 }
74
Popular Tags