http访问页面

来源:互联网 发布:linux 换用户登录 编辑:程序博客网 时间:2024/05/01 15:51
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;}

原创粉丝点击