1 15 16 package com.jdon.bussinessproxy.remote.http; 17 18 import com.jdon.util.Debug; 19 import com.jdon.bussinessproxy.remote.http.util.Base64; 20 21 import java.io.*; 22 import java.net.*; 23 import java.util.*; 24 25 public class HttpConnectionHelper { 26 27 public final static String module = HttpConnectionHelper.class.getName(); 28 29 private static final String MIME_TYPE = 30 ""; 31 32 38 public HttpURLConnection connectService(HttpServerParam httpServerParam, 39 String userPassword) throws 40 Exception { 41 HttpURLConnection httpURLConnection = null; 42 URL url = null; 43 try { 44 45 url = new URL("http", httpServerParam.getHost(), 46 httpServerParam.getPort(), httpServerParam.getServletPath()); 47 48 Debug.logVerbose("[JdonFramework]Service url=" + url, module); 49 50 httpURLConnection = (HttpURLConnection) url.openConnection(); 51 httpURLConnection.setRequestMethod("POST"); 52 httpURLConnection.setDoOutput(true); 53 httpURLConnection.setDoInput(true); 54 httpURLConnection.setUseCaches(false); 55 httpURLConnection.setRequestProperty("Content-Type", 56 "application/x-java-serialized-object"); 57 58 if ((userPassword != null) && (!userPassword.equals("null"))){ 59 String encoded = "Basic " + Base64.encode(userPassword.getBytes("UTF-8")); 60 httpURLConnection.setRequestProperty("Authorization", encoded); 61 } 62 } catch (Exception ex) { 63 Debug.logError("[JdonFramework] connectServer " + url + " error: " + ex, module); 64 throw new Exception (ex); 65 } 66 return httpURLConnection; 67 } 68 69 75 public HttpURLConnection connectLogin(HttpServerParam httpServerParam, 76 String UserPassword) throws 77 Exception { 78 79 HttpURLConnection httpURLConnection = null; 80 URL url = null; 81 try { 82 83 url = new URL("http", httpServerParam.getHost(), 84 httpServerParam.getPort(), httpServerParam.getLoginPath()); 85 86 Debug.logVerbose("[JdonFramework]login url=" + url, module); 87 88 httpURLConnection = (HttpURLConnection) url.openConnection(); 89 httpURLConnection.setRequestMethod("POST"); 90 httpURLConnection.setDoOutput(true); 91 httpURLConnection.setDoInput(true); 92 httpURLConnection.setUseCaches(false); 93 httpURLConnection.setRequestProperty("Content-Type", 94 "application/x-www-form-urlencoded"); 95 96 String encoded = "Basic " + Base64.encode(UserPassword.getBytes("UTF-8")); 97 httpURLConnection.setRequestProperty("Authorization", encoded); 98 99 } catch (Exception ex) { 100 Debug.logError("[JdonFramework] connectServer " + url + " error: " + ex, module); 101 throw new Exception (ex); 102 } 103 return httpURLConnection; 104 } 105 106 112 public void sendObjectRequest(HttpURLConnection httpURLConnection, 113 Object request) throws Exception { 114 try { 115 116 ObjectOutputStream oos = new ObjectOutputStream( 118 httpURLConnection.getOutputStream()); 119 oos.writeObject(request); 120 oos.close(); 121 } catch (Exception ex) { 122 Debug.logError(ex, module); 123 throw new Exception (ex); 124 } 125 } 126 127 133 public void sendDataRequest(HttpURLConnection httpURLConnection, 134 Hashtable param) throws Exception { 135 try { 136 PrintWriter out = new PrintWriter(new OutputStreamWriter( 138 httpURLConnection.getOutputStream())); 139 142 String paramString = ""; 143 for (Enumeration e = param.keys(); e.hasMoreElements(); ) { 144 String key = (String ) e.nextElement(); 145 String value = (String ) param.get(key); 146 paramString += key + "=" + URLEncoder.encode(value, "UTF-8") + "&"; 148 } 149 paramString = paramString.substring(0, paramString.length() - 1); 150 151 154 out.println(paramString); 155 out.close(); 156 } catch (Exception ex) { 157 Debug.logError(ex, module); 158 throw new Exception (ex); 159 } 160 161 } 162 163 169 public Object getObjectResponse(HttpURLConnection httpURLConnection) throws 170 Exception { 171 Object object = null; 172 try { 173 ObjectInputStream ois = new ObjectInputStream( 174 httpURLConnection.getInputStream()); 175 object = ois.readObject(); 176 ois.close(); 177 } catch (Exception ex) { 178 Debug.logError(ex, module); 179 throw new Exception (ex); 180 } 181 return object; 182 } 183 184 public String getStringResponse(HttpURLConnection httpURLConnection) throws 185 Exception { 186 StringBuffer sb = new StringBuffer (); 187 try { 188 BufferedReader in = new BufferedReader(new InputStreamReader( 190 httpURLConnection.getInputStream())); 191 String buffer = ""; 192 while ( (buffer = in.readLine()) != null) { 193 sb.append(buffer); 194 Debug.logVerbose(buffer, module); 195 } 196 in.close(); 197 } catch (Exception ex) { 198 Debug.logError(ex, module); 199 throw new Exception (ex); 200 } 201 return sb.toString(); 202 } 203 204 } 205 | Popular Tags |