1 22 package org.objectweb.speedo.runtime.jmx; 23 24 import java.io.BufferedReader ; 25 import java.io.InputStreamReader ; 26 import java.io.OutputStream ; 27 import java.net.HttpURLConnection ; 28 import java.net.URL ; 29 import java.net.URLConnection ; 30 import java.util.Enumeration ; 31 import java.util.Hashtable ; 32 import java.util.StringTokenizer ; 33 34 import org.objectweb.util.monolog.api.BasicLevel; 35 import org.objectweb.util.monolog.api.Logger; 36 37 41 public class HTTPTools { 42 static String redirectURL; 43 44 47 static Hashtable cookies = null; 48 49 59 public static String getURL( 60 String urlString, 61 String paramString, 62 String method, 63 Logger logger) 64 throws Exception { 65 if (cookies == null) { 67 cookies = new Hashtable (); 69 } 71 72 String s = "Ok"; 74 try { 75 if (!method.equalsIgnoreCase("post") && !paramString.equals("")) { 76 urlString = urlString + "?" + paramString; 77 } 78 79 URL url = new URL (urlString); 80 81 HttpURLConnection conn = (HttpURLConnection ) url.openConnection(); 82 83 conn.setRequestProperty("accept-language", "fr"); 89 conn.setRequestProperty( 90 "user-agent", 91 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)"); 92 93 conn.setRequestProperty ("connection", "Keep-Alive"); 94 96 HttpURLConnection.setFollowRedirects(false); 98 conn.setUseCaches(false); 100 101 setCookies(conn); 102 103 if (method.equalsIgnoreCase("post")) { 104 byte[] bytes = paramString.getBytes(); 105 conn.setDoOutput(true); 106 conn.setDoInput(true); 107 conn.setRequestMethod("POST"); 108 conn.setRequestProperty( 109 "Content-length", 110 String.valueOf(bytes.length)); 111 OutputStream out = conn.getOutputStream(); 112 out.write(bytes); 113 out.flush(); 114 } 115 116 s = "" + conn.getResponseCode(); 117 118 if (conn.getResponseCode() != 200 119 && conn.getResponseCode() != 302) { 120 logger.log(BasicLevel.ERROR, "error " + conn.getResponseCode()); 121 } else if (conn.getResponseCode() == 200) { BufferedReader is = 124 new BufferedReader ( 125 new InputStreamReader (conn.getInputStream())); 126 String line = null; 127 StringBuffer sourceTxt = new StringBuffer (""); 128 while ((line = is.readLine()) != null) 129 sourceTxt.append(line); 130 is.close(); 131 s = sourceTxt.toString(); 132 } 133 getCookies(conn); 134 logger.log( 135 BasicLevel.DEBUG, 136 urlString + "\treturns:" + conn.getResponseCode() + " "); 137 138 while (s.equals("302")) { 139 s = getURL(redirectURL, "", "", logger); 140 } 141 142 if (conn.getResponseCode() == 500) 143 throw new Exception ("the URL " + url + " fails."); 144 145 if (conn.getResponseCode() == 404) 146 throw new Exception ("the URL " + url + " not found."); 147 148 } catch (Exception ex) { 149 s = ""; 150 logger.log(BasicLevel.DEBUG, urlString + " throws Execption!", ex); 151 throw ex; 152 } 153 154 return s; 155 } 156 157 162 private static void getCookies(HttpURLConnection con) { 163 int n = 1; 164 165 label0 : for (boolean done = false; !done; n++) { 166 String headerKey = con.getHeaderFieldKey(n); 167 String headerVal = con.getHeaderField(n); 168 if (headerKey != null || headerVal != null) { 169 if ("location".equalsIgnoreCase(headerKey)) { 170 redirectURL = headerVal; 171 } 172 if (!"Set-Cookie".equals(headerKey)) 173 continue; 174 StringTokenizer st = new StringTokenizer (headerVal, ";"); 175 do { 176 if (!st.hasMoreTokens()) 177 continue label0; 178 String pair = st.nextToken(); 179 StringTokenizer stt = new StringTokenizer (pair, "="); 180 while (stt.hasMoreTokens()) { 181 String cookName = stt.nextToken(); 182 String cookValue = ""; 183 try { 184 cookValue = stt.nextToken(); 185 } catch (Exception exception) { 186 } 187 if (!cookName.trim().equalsIgnoreCase("path") 188 && !cookName.trim().equalsIgnoreCase("expires")) { 189 cookies.put(cookName.trim(), cookValue); 192 } 193 } 194 } 195 while (true); 196 } 197 done = true; 198 } 199 } 200 201 207 private static void setCookies(URLConnection con) { 208 Enumeration en = cookies.keys(); 209 String cookieString; 210 String key = ""; 211 String val = ""; 212 for (cookieString = ""; 213 en.hasMoreElements(); 214 cookieString = cookieString + (key + "=" + val + ";")) { 215 key = (String ) en.nextElement(); 216 val = (String ) cookies.get(key); 217 } 218 con.setRequestProperty("Cookie", cookieString); 220 } 221 } 222 | Popular Tags |