KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > bussinessproxy > remote > http > HttpConnectionHelper


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

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 JavaDoc module = HttpConnectionHelper.class.getName();
28
29   private static final String JavaDoc MIME_TYPE =
30       "";
31
32   /**
33    * 连接Http Server, 准备传é€?serialized-object
34    * @param httpServerParam
35    * @return
36    * @throws java.lang.Exception
37    */

38   public HttpURLConnection connectService(HttpServerParam httpServerParam,
39                                           String JavaDoc userPassword) throws
40       Exception JavaDoc {
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 JavaDoc encoded = "Basic " + Base64.encode(userPassword.getBytes("UTF-8"));
60           httpURLConnection.setRequestProperty("Authorization", encoded);
61       }
62     } catch (Exception JavaDoc ex) {
63       Debug.logError("[JdonFramework] connectServer " + url + " error: " + ex, module);
64       throw new Exception JavaDoc(ex);
65     }
66     return httpURLConnection;
67   }
68
69   /**
70    * 连接Http Server, 准备传é€?HttpServletRequest
71    * @param httpServerParam
72    * @return
73    * @throws java.lang.Exception
74    */

75   public HttpURLConnection connectLogin(HttpServerParam httpServerParam,
76                                         String JavaDoc UserPassword) throws
77       Exception JavaDoc {
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 JavaDoc encoded = "Basic " + Base64.encode(UserPassword.getBytes("UTF-8"));
97       httpURLConnection.setRequestProperty("Authorization", encoded);
98
99     } catch (Exception JavaDoc ex) {
100       Debug.logError("[JdonFramework] connectServer " + url + " error: " + ex, module);
101       throw new Exception JavaDoc(ex);
102     }
103     return httpURLConnection;
104   }
105
106   /**
107    * å°†å?¯åº?列化Objectå?‘å¾€Http Server
108    * @param httpURLConnection
109    * @param request
110    * @throws java.lang.Exception
111    */

112   public void sendObjectRequest(HttpURLConnection httpURLConnection,
113                                 Object JavaDoc request) throws Exception JavaDoc {
114     try {
115
116       //send the request query object to the server
117
ObjectOutputStream oos = new ObjectOutputStream(
118           httpURLConnection.getOutputStream());
119       oos.writeObject(request);
120       oos.close();
121     } catch (Exception JavaDoc ex) {
122       Debug.logError(ex, module);
123       throw new Exception JavaDoc(ex);
124     }
125   }
126
127   /**
128    * 将字符串å?‘å¾€Http
129    * @param httpURLConnection
130    * @param sendText
131    * @throws java.lang.Exception
132    */

133   public void sendDataRequest(HttpURLConnection httpURLConnection,
134                               Hashtable param) throws Exception JavaDoc {
135     try {
136       // write out parameters, assume parameters are put in the hashtable
137
PrintWriter out = new PrintWriter(new OutputStreamWriter(
138           httpURLConnection.getOutputStream()));
139       // open up the output stream of the connection
140
// DataOutputStream output = new DataOutputStream(conn.getOutputStream() );
141

142       String JavaDoc paramString = "";
143       for (Enumeration e = param.keys(); e.hasMoreElements(); ) {
144         String JavaDoc key = (String JavaDoc) e.nextElement();
145         String JavaDoc value = (String JavaDoc) param.get(key);
146         // no harm for an extra & at the end of the parameter list
147
paramString += key + "=" + URLEncoder.encode(value, "UTF-8") + "&";
148       }
149       paramString = paramString.substring(0, paramString.length() - 1);
150
151 // output.writeBytes( paramString );
152
// output.close();
153

154       out.println(paramString);
155       out.close();
156     } catch (Exception JavaDoc ex) {
157       Debug.logError(ex, module);
158       throw new Exception JavaDoc(ex);
159     }
160
161   }
162
163   /**
164    * 从回å¤?中获å?–Object
165    * @param httpURLConnection
166    * @return
167    * @throws java.lang.Exception
168    */

169   public Object JavaDoc getObjectResponse(HttpURLConnection httpURLConnection) throws
170       Exception JavaDoc {
171     Object JavaDoc object = null;
172     try {
173       ObjectInputStream ois = new ObjectInputStream(
174           httpURLConnection.getInputStream());
175       object = ois.readObject();
176       ois.close();
177     } catch (Exception JavaDoc ex) {
178       Debug.logError(ex, module);
179       throw new Exception JavaDoc(ex);
180     }
181     return object;
182   }
183
184   public String JavaDoc getStringResponse(HttpURLConnection httpURLConnection) throws
185       Exception JavaDoc {
186     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
187     try {
188       //测试时打å?°
189
BufferedReader in = new BufferedReader(new InputStreamReader(
190           httpURLConnection.getInputStream()));
191       String JavaDoc buffer = "";
192       while ( (buffer = in.readLine()) != null) {
193         sb.append(buffer);
194         Debug.logVerbose(buffer, module);
195       }
196       in.close();
197     } catch (Exception JavaDoc ex) {
198       Debug.logError(ex, module);
199       throw new Exception JavaDoc(ex);
200     }
201     return sb.toString();
202   }
203
204 }
205
Popular Tags