public class UrlDispatcherUtil {
private HttpClient client = new HttpClient();
public UrlDispatcherUtil(String host, int port, String protocol){
client.getHostConfiguration().setHost(host, port, protocol);
}
public String getRequest(String url) throws Exception{
String destUrl = url.replaceAll("%26", "&");
URI uri = new URI(destUrl,false,"UTF-8");
GetMethod get = new GetMethod( uri.toString() );
client.executeMethod(get);
String r = get.getResponseBodyAsString();
get.releaseConnection();
return r;
}
public void doGet( HttpServletRequest req, HttpServletResponse resp ) throws URIException, NullPointerException{
String queryPath = req.getRequestURI();
StringBuffer sb = new StringBuffer(queryPath);
Set<String > keys = req.getParameterMap().keySet();
sb.append("?");
for(String key : keys){
sb.append(key).append("=").append(req.getParameter(key)).append("&");
}
sb.append("t=").append(System.currentTimeMillis());
queryPath = "/nsa"+sb.toString();
System.out.println(queryPath);
String destUrl = queryPath.replaceAll("%26", "&");
URI uri = new URI(destUrl,false,"UTF-8");
GetMethod get = new GetMethod( uri.toString());
try {
client.executeMethod(get);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
try {
for( Header header : get.getRequestHeaders() ){
resp.addHeader(header.getName(), header.getValue() );
}
resp.setCharacterEncoding(get.getResponseCharSet());
System.out.println(get.getResponseCharSet());
Streams.copy(get.getResponseBodyAsStream(), resp.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
resp.setStatus( get.getStatusCode() );
get.releaseConnection();
}
public void doPost( HttpServletRequest req, HttpServletResponse resp ) throws NullPointerException, HttpException, IOException{
System.out.println("dopost begin --------------------------");
String queryPath = req.getRequestURI();
StringBuffer sb = new StringBuffer("/nsa").append(queryPath);
Set<String > keys = req.getParameterMap().keySet();
URI uri = new URI(sb.toString(),false,"UTF-8");
System.out.println("dopost begin --------------------------"+uri.toString());
PostMethod pt = new PostMethod( uri.toString() );
List<NameValuePair> ptParams = new ArrayList<NameValuePair>();
for(String key : keys ){
NameValuePair pair = new NameValuePair(key, req.getParameter(key));
ptParams.add(pair);
}
NameValuePair[] pa = new NameValuePair[keys.size()];
ptParams.toArray(pa);
pt.setRequestBody( pa );
int statusCode = client.executeMethod(pt);
System.out.println(statusCode+"+++++++++++++++++++++++++++++++++");
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
Header locationHeader = pt.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out.println("The page was redirected to:" + location);
} else {
System.err.println("Location field value is null.");
}
}
for( Header header : pt.getRequestHeaders() ){
resp.addHeader(header.getName(), header.getValue() );
}
resp.setStatus( pt.getStatusCode() );
resp.setCharacterEncoding(pt.getResponseCharSet());
System.out.println(pt.getResponseCharSet());
Streams.copy(pt.getResponseBodyAsStream(), resp.getOutputStream());
pt.releaseConnection();
}
public String postRequest(String url, Map<String,String> params) throws Exception {
PostMethod pt = new PostMethod( url );
List<NameValuePair> ptParams = new ArrayList<NameValuePair>();
for(String key : params.keySet() ){
NameValuePair pair = new NameValuePair(key, params.get(key));
ptParams.add(pair);
}
NameValuePair[] pa = new NameValuePair[params.size()];
ptParams.toArray(pa);
pt.setRequestBody( pa );
int statusCode = client.executeMethod(pt);
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
Header locationHeader = pt.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out.println("The page was redirected to:" + location);
} else {
System.err.println("Location field value is null.");
}
}
String r = pt.getResponseBodyAsString();
pt.releaseConnection();
return r;
}
} :)
HttpClent
最新推荐文章于 2022-05-01 15:37:21 发布