Revision 5914
Added by ben leinfelder about 13 years ago
RequestUtil.java | ||
---|---|---|
26 | 26 |
|
27 | 27 |
package edu.ucsb.nceas.metacat.util; |
28 | 28 |
|
29 |
import java.io.InputStream; |
|
29 | 30 |
import java.io.InputStreamReader; |
30 | 31 |
import java.io.IOException; |
31 | 32 |
import java.io.BufferedReader; |
... | ... | |
33 | 34 |
import java.net.MalformedURLException; |
34 | 35 |
import java.net.URL; |
35 | 36 |
import java.net.URLConnection; |
37 |
import java.util.ArrayList; |
|
36 | 38 |
import java.util.Enumeration; |
37 | 39 |
import java.util.HashMap; |
38 | 40 |
import java.util.Hashtable; |
41 |
import java.util.Iterator; |
|
42 |
import java.util.List; |
|
39 | 43 |
import java.util.Set; |
40 | 44 |
import java.util.Vector; |
41 | 45 |
|
... | ... | |
46 | 50 |
import javax.servlet.http.HttpServletResponse; |
47 | 51 |
import javax.servlet.http.HttpSession; |
48 | 52 |
|
49 |
import org.apache.commons.httpclient.HttpClient; |
|
50 |
import org.apache.commons.httpclient.HttpException; |
|
51 |
import org.apache.commons.httpclient.methods.PostMethod; |
|
53 |
import org.apache.commons.io.IOUtils; |
|
54 |
import org.apache.http.HttpException; |
|
55 |
import org.apache.http.HttpResponse; |
|
56 |
import org.apache.http.HttpVersion; |
|
57 |
import org.apache.http.NameValuePair; |
|
58 |
import org.apache.http.client.HttpClient; |
|
59 |
import org.apache.http.client.entity.UrlEncodedFormEntity; |
|
60 |
import org.apache.http.client.methods.HttpPost; |
|
61 |
import org.apache.http.impl.client.DefaultHttpClient; |
|
62 |
import org.apache.http.message.BasicNameValuePair; |
|
63 |
import org.apache.http.params.CoreProtocolPNames; |
|
52 | 64 |
import org.apache.log4j.Logger; |
53 | 65 |
|
54 | 66 |
import edu.ucsb.nceas.metacat.properties.PropertyService; |
... | ... | |
59 | 71 |
public class RequestUtil { |
60 | 72 |
|
61 | 73 |
private static Logger logMetacat = Logger.getLogger(RequestUtil.class); |
74 |
private static String encoding = "UTF-8"; |
|
62 | 75 |
|
63 | 76 |
/** |
64 | 77 |
* private constructor - all methods are static so there is no |
... | ... | |
169 | 182 |
* map of parameters to add to the post |
170 | 183 |
* @returns a string holding the response body |
171 | 184 |
*/ |
172 |
public static String post(HttpClient httpClient, String url,
|
|
185 |
public static String post(HttpClient httpclient, String url,
|
|
173 | 186 |
HashMap<String, String> paramMap) throws IOException, HttpException { |
174 | 187 |
|
175 |
PostMethod method = new PostMethod(url); |
|
176 |
|
|
177 |
// Configure the form parameters |
|
178 |
if (paramMap != null) { |
|
179 |
Set<String> paramNames = paramMap.keySet(); |
|
180 |
for (String paramName : paramNames) { |
|
181 |
method.addParameter(paramName, paramMap.get(paramName)); |
|
182 |
} |
|
183 |
} |
|
184 |
|
|
185 |
// Execute the POST method |
|
186 |
int statusCode = httpClient.executeMethod(method); |
|
187 |
if (statusCode != -1) { |
|
188 |
String contents = method.getResponseBodyAsString(); |
|
189 |
method.releaseConnection(); |
|
190 |
return (contents); |
|
191 |
} |
|
192 |
|
|
188 |
httpclient.getParams().setParameter( |
|
189 |
CoreProtocolPNames.PROTOCOL_VERSION, |
|
190 |
HttpVersion.HTTP_1_1); |
|
191 |
httpclient.getParams().setParameter( |
|
192 |
CoreProtocolPNames.HTTP_CONTENT_CHARSET, |
|
193 |
encoding ); |
|
194 |
HttpPost post = new HttpPost(url); |
|
195 |
//set the params |
|
196 |
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); |
|
197 |
Iterator<String> keys = paramMap.keySet().iterator(); |
|
198 |
while (keys.hasNext()) { |
|
199 |
String key = keys.next(); |
|
200 |
String value = paramMap.get(key); |
|
201 |
NameValuePair nvp = new BasicNameValuePair(key, value); |
|
202 |
nameValuePairs.add(nvp); |
|
203 |
} |
|
204 |
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, encoding)); |
|
205 |
//post.setHeader("Cookie", "JSESSIONID="+ sessionId); |
|
206 |
HttpResponse httpResponse = httpclient.execute(post); |
|
207 |
if (httpResponse.getStatusLine().getStatusCode() != -1) { |
|
208 |
InputStream result = httpResponse.getEntity().getContent(); |
|
209 |
String contents = IOUtils.toString(result, encoding); |
|
210 |
return contents; |
|
211 |
} |
|
212 |
|
|
193 | 213 |
return null; |
194 | 214 |
} |
195 | 215 |
|
Also available in: Unified diff
remove httpclient 3.1 and custom-built httpclient.jar
rework MetacatClient (and other classes) to use httpclient 4
updated build to not create httpclient.jar
encoding tests now pass.