国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

HttpClient4.3使用經驗(一)簡單使用

2019-11-14 15:02:24
字體:
來源:轉載
供稿:網友
package com.wp.nevel.base.utils;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.SocketException;import java.net.UnknownHostException;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.ClientPRotocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.utils.URLEncodedUtils;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.protocol.HttpContext;import org.apache.http.util.EntityUtils;import org.apache.log4j.Logger;import org.junit.Test;import com.wp.nevel.base.exception.ParserException;import com.wp.nevel.base.service.impl.LogServiceHelp;public class HttpClientUtils  {    public static  Logger logger = Logger.getLogger( LogServiceHelp.class);        private static HttpClient httpclient;    static {        httpclient = HttpClients.createDefault();    }        @Test    public void test(){        String url="http://www.shuchongw.com/files/article/html/23/23114/index.html";        doGetHtmlContent2byte(url);    }                /**     * 根據簡單url獲取網頁數據,轉換成byte [] 存儲     * */    public static byte[] doGetHtmlContent2byte(String url) {        CloseableHttpResponse response = null;        byte[] resultByte = {};        try {            HttpGet get = new HttpGet(url);            System.out.println(url);            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();            get.setConfig(requestConfig);            try {                response = (CloseableHttpResponse) HttpClientUtils.httpclient.execute(get);            } catch (UnknownHostException e) {                e.printStackTrace();                    logger.info("鏈接主網失敗");            }            int statusCode = response.getStatusLine().getStatusCode();            System.out.println(statusCode);            if (statusCode == 200) {                HttpEntity entity = response.getEntity();                resultByte = EntityUtils.toByteArray(entity);            }        } catch (ClientProtocolException e) {            e.printStackTrace();        } catch (SocketException e) {            e.printStackTrace();        }catch(IOException e){            e.printStackTrace();        } finally {            try {                if(response!=null){                    response.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }        return resultByte;    }        /**     * 根據復雜url獲取網頁數據,轉換成byte [] 存儲     * @throws ParserException      * @throws IOException      * @throws UnknownHostException      * @throws ClientProtocolException      * @throws SocketException      * */    public static byte [] doGetHtmlByParams2Byte(Map<String, String> params, String paramsEncoding, String url)  {        if (params != null) {            List<NameValuePair> formparams = new ArrayList<NameValuePair>();            for (Entry<String, String> entry : params.entrySet()) {                formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));            }            if (!formparams.isEmpty()) {                String paramsStr = URLEncodedUtils.format(formparams, paramsEncoding!=null?paramsEncoding:"utf-8");                url = url + "?" + paramsStr;            }        }        return doGetHtmlContent2byte(url);    }        /**     * 根據復雜url獲取網頁數據,轉換成String 存儲     * @throws ParserException      * @throws IOException      * @throws UnknownHostException      * @throws ClientProtocolException      * @throws SocketException      * */    public static String doGetHtmlByParams2Text(Map<String, String> params, String paramsEncoding, String url,String htmlEncoding) throws  ClientProtocolException, UnknownHostException, SocketException{        try {            return getHtmlContentByText(doGetHtmlByParams2Byte(params,paramsEncoding,url),htmlEncoding!=null?htmlEncoding:"utf-8");        } catch (Exception e) {            e.printStackTrace();            return "";        }    }        /**     * 根據簡單url獲取網頁數據,轉換成String 存儲     * @throws ParserException      * @throws IOException      * @throws UnknownHostException      * @throws ClientProtocolException      * @throws SocketException      * */    public static String doGetHtmlContentToString(String url, String encoding){        try {            return getHtmlContentByText(doGetHtmlContent2byte(url),encoding);        } catch (Exception e) {            e.printStackTrace();            return "";        }    }    /**     * 根據簡單url獲取網頁圖片數據, 保存路徑[saveImagePath]     * @throws ParserException      * @throws IOException      * @throws UnknownHostException      * @throws ClientProtocolException      * @throws SocketException      * */    public static  void getHtml2Image(String url,String saveImagPath){        try {            downloadData(doGetHtmlContent2byte(url),saveImagPath);        } catch (Exception e) {            e.printStackTrace();        }    }        public static  String getHtmlContentByText(byte [] htmlBytes, String encoding){        try {            return new String (htmlBytes,encoding!=null?encoding:"utf-8");        } catch (UnsupportedEncodingException e) {            e.printStackTrace();            return  "";        }    }            /**     * 執行下載io     *      * @param byte [] data 網頁字節流,filename 存儲地址和文件名 return     * */    public static void downloadData(byte[] data, String filename) {        try {            DataOutputStream writer = new DataOutputStream(                    new FileOutputStream(new File(filename)));            BufferedOutputStream out = new BufferedOutputStream(writer);            out.write(data);            out.flush();            out.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }        public static String readFile(String filename) throws IOException{        String xmlContent =null;        File file = new File(filename);        BufferedReader reader =null;        try {            if(!file.exists()){                 new RuntimeException("文件不存在");                 return xmlContent;            }            StringBuilder buider = new StringBuilder();            String readata ="";            reader = new BufferedReader(new FileReader(file));            while(true){                readata = reader.readLine();                if(readata==null){                    break;                }                buider.append(readata).append("/n");            }            xmlContent=buider.toString();        } catch (FileNotFoundException e) {            e.printStackTrace();        }finally{            if(reader!=null)                reader.close();        }        return xmlContent;    }        public static byte []  doGetByteByHttpclient2Url(HttpContext httpContext,CloseableHttpClient client,String url){        byte [] resultBytes = null;        try {            HttpGet get = new HttpGet(url);            CloseableHttpResponse response =client.execute(get, httpContext);            int status = response.getStatusLine().getStatusCode();            System.out.println("鏈接狀態="+status);            if(status!=200)                return resultBytes;            HttpEntity entity = response.getEntity();            resultBytes = EntityUtils.toByteArray(entity);        } catch (ClientProtocolException e) {            throw new RuntimeException("失敗連接地址"+url, e);        } catch (IOException e) {            throw new RuntimeException("失敗連接地址"+url, e);        }        if(resultBytes==null){            try {                Thread.sleep(2000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }        return resultBytes;    }    }

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐都县| 古交市| 彭州市| 太康县| 安龙县| 金秀| 临高县| 罗田县| 金湖县| 玉山县| 兴安县| 乐至县| 湘潭县| 娄底市| 海阳市| 汉沽区| 榆林市| 同江市| 水城县| 景德镇市| 辛集市| 大悟县| 巴青县| 方正县| 金溪县| 衡山县| 福安市| 石台县| 柘城县| 龙海市| 东海县| 平谷区| 公主岭市| 库车县| 类乌齐县| 宜兰市| 昆明市| 文水县| 云梦县| 永登县| 新疆|