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的jar包
HttpClent需要jar包,包含: httpclient-4.3.1.jar httpcore-4.3.jar
HttpClent所需jar包,包含: commons-httpclient-3.1.jar commons-codec-1.3.jar
java使用的HttpClient,远程接口通信jar包,亲测可用解决代码http包报错问题
根据HttpClient写的工具类,包含POST和GET请求,使用了连接池和代理等
这是一个HttpClient jar包, 学习网络框架 就要从下面开始了解, 年轻人不要浮躁
希望用http协议的能可以帮到大家!很有使用价值!大家可以看看!
包含httpclient方式调用webservice所需要的jar包
httpClient 工具类,简化请求开发,拿来即用,我用在SpringBoot框架;对于依赖,你可以根据包名导入。
NULL 博文链接:https://msnvip.iteye.com/blog/351818
重新封装的HttpClient类 用于网页抓取的朋友们可以留着,还算比较完善!
1.6.3使用HttpClent下载一网页 // 1、通过HttpGet获取到response对象 CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://www.baidu.com/"); ...
对京东手机数据爬取,使用httpclent爬取数据,其次使用jsoup解析数据。百分之百可运行,代码很简单通俗易懂
代码中包含httpserver和httpclent等方面,并且添加wpf项目,引入httpsever类库,适合学习socket通讯
httpclient工具类包含get,post,put,delete请求