zoukankan      html  css  js  c++  java
  • 使用HttpClient获取网页源码

    需要的包:commons-codec-1.3.jar commons-httpclient-3.1.jar

    /**
         * 通过get方式取得html
         * @param url
         * @param timeout
         * @param charset
         * @return
         */
        public static String getHTMLByUrl(String url, int timeout, String charset){
            if (charset == null)
              charset = "utf-8";
            HttpMethod result = new GetMethod(url);
            return doHttp(result,timeout,charset);
        }
        
        /**
         * 通过post方式取得html
         * @param url
         * @param timeout
         * @param charset
         * @param params
         * @return
         */
        public static String getHTMLByUrl(String url, int timeout, String charset,Map<String,String> params){
            if (charset == null)
              charset = "utf-8";
            PostMethod result = new PostMethod(url);
            NameValuePair[] nameValues =new NameValuePair[params.size()];
            NameValuePair simcard;
            String[] keys=params.keySet().toArray(new String[params.size()]);
            String key;
            for(int i=0;i<keys.length;i++){
                  key=keys[i];
                  simcard = new NameValuePair(key,params.get(key));
                  nameValues[i]=simcard;
            }
            result.setRequestBody(nameValues);
            return doHttp(result,timeout,charset);
         }
         
         public static String doHttp(HttpMethod result,int timeout, String charset){
                HttpClient client = new HttpClient();
                try {
                      HttpConnectionManagerParams managerParams = client
                        .getHttpConnectionManager().getParams();
    
                      managerParams.setConnectionTimeout(timeout);
                      client.executeMethod(result);          
                      InputStream resStream = result.getResponseBodyAsStream();
                      BufferedReader br = new BufferedReader(new InputStreamReader(resStream, charset));
                      StringBuffer resBuffer = new StringBuffer();
                      String resTemp = "";
                      while ((resTemp = br.readLine()) != null) {
                        resBuffer.append(resTemp);
                      }
                      return resBuffer.toString();
                    }
                    catch (HttpException e)
                    {
                      return null;
                    }
                    catch (IOException e) {
                      return null;
                    }
                    catch (Exception e) {
                      return null;
                    } finally {
                      result.releaseConnection();
                    }
         }
  • 相关阅读:
    Idea中资源文件的设置和Java中类的权限
    Springboot中slf4j+log4j2的使用
    linux下修改文件查看样式(日期/文件大小)
    maven项目中引入外部jar
    01背包问题
    python爬取千千音乐
    【DataBase】SQL优化案例:其一
    【Uni-App】底部栏踩坑
    【Uni-App】UniApp转微信小程序发布应用
    【Uni-App】API笔记 P2
  • 原文地址:https://www.cnblogs.com/zhanghaoh/p/2845467.html
Copyright © 2011-2022 走看看