private static final String HTML_PATH ="\\var\\wwww\\html\\shop\\index.html";
public boolean copyHtml(){
boolean flag = false;
String response =null;
File htmlFile =null;
HttpClient client = new HttpClient();
//主机+端口号+协议,http默认80
client.getHostConfiguration().setHost("shop.ssports.com", 80, "http");
HttpMethod method = new GetMethod();
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());//恢复策略默认重发三次,也可以自定义,实现HttpMethodRetryHandler
try {
int statusCode=client.executeMethod(method);
if(statusCode==HttpStatus.SC_OK){
//状态码如不对,表示访问不成功
response =method.getResponseBodyAsString();
htmlFile =new File(HTML_PATH);
if(htmlFile.exists()) htmlFile.delete();
FileCopyUtils.copy(response.getBytes("UTF-8"), htmlFile);
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
flag = false;
}
//必须要释放连接
method.releaseConnection();
return flag;
}
http访问页面
最新推荐文章于 2023-12-12 22:12:06 发布