获取url资源的几种方式

1:HttpClient的get方式(程序中使用的HttpClient为4.3.1版本)

   public static String doGet(String src,String unicode){
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        InputStream is = null;
        HttpGet get = new HttpGet(src);
        CloseableHttpClient httpclient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();//设置请求和传输超时时间
        get.setConfig(requestConfig);
        String result = null;
        try {
            response = httpclient.execute(get);
            if (null != response && response.getStatusLine().getStatusCode() == 200) {
                entity = response.getEntity();

                //得到流对象
                is = entity.getContent();
                System.out.println(is.toString());
                //得到文档对象
                result = EntityUtils.toString(entity, unicode);
                System.out.println(result);
            }
            if (null != response && (is ==null)) {
                logger.error("sendInfoForGET response err:" + response.getStatusLine() + ",url="+src);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


2HttpClient的post方式+添加表单数据

public static String doPost(String src,Map<String,String> params,String unicode){
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        HttpPost post = new HttpPost(src);
        BasicCookieStore cookies = new BasicCookieStore();
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookies).build();
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();//设置请求和传输超时时间
        post.setConfig(requestConfig);
        // 创建参数队列
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        //注入参数
        for(String key:params.keySet()){
            formparams.add(new BasicNameValuePair(key,params.get(key)));
        }
        UrlEncodedFormEntity uefEntity;
        String result = null;;
        try {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            post.setEntity(uefEntity);
            response = httpclient.execute(post);
            if (null != response && response.getStatusLine().getStatusCode() == 200) {
                entity = response.getEntity();
                List<org.apache.http.cookie.Cookie> list = cookies.getCookies();
                for (int i = 0; i < list.size(); i++) {
                    System.out.println("-"+list.get(i).toString());
                }
            }
            if (null != response ) {
                logger.error("sendInfoForGET response err:" + response.getStatusLine() + ",url="+src);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


3.使用java.net.url获取资源(一般用来获取图片资源,编码没有处理正确)

public static void urlConnect(String src){
        URL   url = null;
        try {
            url = new URL(src);
            URLConnection con = url.openConnection();
            con.setConnectTimeout(5*1000);
            con.setReadTimeout(5*1000);
            //获取流对象
            InputStream is = con.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader bi = new BufferedReader(isr);
            String tempLine = null;
            StringBuffer sb = new StringBuffer();
            while((tempLine=bi.readLine())!=null){
                tempLine = new String(tempLine.getBytes(),"gb2312");
                sb.append(tempLine+"\n");
            }
            System.out.println(sb.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

4.使用jsoup获取文档

  public static void connectByJsoup(String url){
        Connection conn = Jsoup.connect(url);
        //忽略文档类型
        conn.ignoreContentType(true);
        //设置请求来源,针对防盗链
        conn.referrer("www.baidu.com");
        //增加cookie
        try {
            Document doc = conn.cookies(new HashMap<>()).get();
            System.out.println(doc.html());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值