Revision 4201
Added by daigle over 16 years ago
src/edu/ucsb/nceas/metacat/util/RequestUtil.java | ||
---|---|---|
27 | 27 |
package edu.ucsb.nceas.metacat.util; |
28 | 28 |
|
29 | 29 |
import java.io.IOException; |
30 |
import java.util.HashMap; |
|
31 |
import java.util.Set; |
|
30 | 32 |
import java.util.Vector; |
31 | 33 |
|
32 | 34 |
import javax.servlet.ServletContext; |
... | ... | |
34 | 36 |
import javax.servlet.http.HttpServletRequest; |
35 | 37 |
import javax.servlet.http.HttpServletResponse; |
36 | 38 |
|
39 |
import org.apache.commons.httpclient.HttpClient; |
|
40 |
import org.apache.commons.httpclient.HttpException; |
|
41 |
import org.apache.commons.httpclient.methods.PostMethod; |
|
37 | 42 |
import org.apache.log4j.Logger; |
38 | 43 |
|
39 | 44 |
public class RequestUtil { |
... | ... | |
68 | 73 |
servletContext.getRequestDispatcher(destination).forward(request, |
69 | 74 |
response); |
70 | 75 |
} |
76 |
|
|
77 |
/** |
|
78 |
* Post a request and return the response body |
|
79 |
* |
|
80 |
* @param httpClient |
|
81 |
* The HttpClient to use in the post. This is passed in because |
|
82 |
* the same client may be used in several posts |
|
83 |
* @param url |
|
84 |
* the url to post to |
|
85 |
* @param paramMap |
|
86 |
* map of parameters to add to the post |
|
87 |
* @returns a string holding the response body |
|
88 |
*/ |
|
89 |
public static String post(HttpClient httpClient, String url, |
|
90 |
HashMap<String, String> paramMap) throws IOException, HttpException { |
|
91 |
|
|
92 |
PostMethod method = new PostMethod(url); |
|
93 |
|
|
94 |
// Configure the form parameters |
|
95 |
if (paramMap != null) { |
|
96 |
Set<String> paramNames = paramMap.keySet(); |
|
97 |
for (String paramName : paramNames) { |
|
98 |
method.addParameter(paramName, paramMap.get(paramName)); |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
// Execute the POST method |
|
103 |
int statusCode = httpClient.executeMethod(method); |
|
104 |
if (statusCode != -1) { |
|
105 |
String contents = method.getResponseBodyAsString(); |
|
106 |
method.releaseConnection(); |
|
107 |
return (contents); |
|
108 |
} |
|
109 |
|
|
110 |
return null; |
|
111 |
} |
|
112 |
|
|
71 | 113 |
|
72 | 114 |
/** |
73 | 115 |
* Add a list of errors to the request. The pages will pick up the errors |
Also available in: Unified diff
Add a post method