1 22 package org.objectweb.speedo.j2eedo.common; 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 static TraceTime traceTime = new TraceTime(); 49 50 60 public static String getURL( 61 String urlString, 62 String paramString, 63 String method, 64 Logger logger) 65 throws Exception { 66 if (cookies == null) { 68 cookies = new Hashtable (); 70 } 72 73 String s = "Ok"; 75 try { 76 if (!method.equalsIgnoreCase("post") && !paramString.equals("")) { 77 urlString = urlString + "?" + paramString; 78 } 79 80 URL url = new URL (urlString); 81 82 traceTime.startTask("openConnection"); 83 HttpURLConnection conn = (HttpURLConnection ) url.openConnection(); 84 traceTime.endTask(); 85 86 conn.setRequestProperty("accept-language", "fr"); 92 conn.setRequestProperty( 93 "user-agent", 94 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)"); 95 96 conn.setRequestProperty ("connection", "Keep-Alive"); 97 99 HttpURLConnection.setFollowRedirects(false); 101 conn.setUseCaches(false); 103 104 setCookies(conn); 105 106 if (method.equalsIgnoreCase("post")) { 107 traceTime.startTask("set post parameters"); 108 byte[] bytes = paramString.getBytes(); 109 conn.setDoOutput(true); 110 conn.setDoInput(true); 111 conn.setRequestMethod("POST"); 112 conn.setRequestProperty( 113 "Content-length", 114 String.valueOf(bytes.length)); 115 OutputStream out = conn.getOutputStream(); 116 out.write(bytes); 117 out.flush(); 118 traceTime.endTask(); 119 } 120 121 traceTime.startTask("getResponseCode"); 122 s = "" + conn.getResponseCode(); 123 traceTime.endTask(); 124 125 if (conn.getResponseCode() != 200 126 && conn.getResponseCode() != 302) { 127 logger.log(BasicLevel.ERROR, "error " + conn.getResponseCode()); 128 } else if (conn.getResponseCode() == 200) { BufferedReader is = 131 new BufferedReader ( 132 new InputStreamReader (conn.getInputStream())); 133 String line = null; 134 StringBuffer sourceTxt = new StringBuffer (""); 135 while ((line = is.readLine()) != null) 136 sourceTxt.append(line); 137 is.close(); 138 s = sourceTxt.toString(); 139 } 140 getCookies(conn); 141 logger.log( 142 BasicLevel.DEBUG, 143 urlString + "\treturns:" + conn.getResponseCode() + " "); 144 if (logger.isLoggable(BasicLevel.DEBUG)) { 145 logger.log(BasicLevel.DEBUG, traceTime.report()); 146 } 147 148 while (s.equals("302")) { 149 s = getURL(redirectURL, "", "", logger); 150 } 151 152 if (conn.getResponseCode() == 500) 153 throw new Exception ("the URL " + url + " fails."); 154 155 if (conn.getResponseCode() == 404) 156 throw new Exception ("the URL " + url + " not found."); 157 158 } catch (Exception ex) { 159 s = ""; 160 logger.log(BasicLevel.DEBUG, urlString + " throws Execption!", ex); 161 throw ex; 162 } finally { 163 traceTime.reset(); 164 } 165 166 return s; 167 } 168 169 174 private static void getCookies(HttpURLConnection con) { 175 int n = 1; 176 177 label0 : for (boolean done = false; !done; n++) { 178 String headerKey = con.getHeaderFieldKey(n); 179 String headerVal = con.getHeaderField(n); 180 if (headerKey != null || headerVal != null) { 181 if ("location".equalsIgnoreCase(headerKey)) { 182 redirectURL = headerVal; 183 } 184 if (!"Set-Cookie".equals(headerKey)) 185 continue; 186 StringTokenizer st = new StringTokenizer (headerVal, ";"); 187 do { 188 if (!st.hasMoreTokens()) 189 continue label0; 190 String pair = st.nextToken(); 191 StringTokenizer stt = new StringTokenizer (pair, "="); 192 while (stt.hasMoreTokens()) { 193 String cookName = stt.nextToken(); 194 String cookValue = ""; 195 try { 196 cookValue = stt.nextToken(); 197 } catch (Exception exception) { 198 } 199 if (!cookName.trim().equalsIgnoreCase("path") 200 && !cookName.trim().equalsIgnoreCase("expires")) { 201 cookies.put(cookName.trim(), cookValue); 204 } 205 } 206 } 207 while (true); 208 } 209 done = true; 210 } 211 } 212 213 219 private static void setCookies(URLConnection con) { 220 Enumeration en = cookies.keys(); 221 String cookieString; 222 String key = ""; 223 String val = ""; 224 for (cookieString = ""; 225 en.hasMoreElements(); 226 cookieString = cookieString + (key + "=" + val + ";")) { 227 key = (String ) en.nextElement(); 228 val = (String ) cookies.get(key); 229 } 230 con.setRequestProperty("Cookie", cookieString); 232 } 233 } 234 | Popular Tags |