中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

用HtppClient下載一個(gè)網(wǎng)頁的完整代碼

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
 import java.io.IOException;  
import java.io.InputStream;  
import java.io.PrintWriter;  
import java.io.Writer;  
import java.util.Scanner;  
  
import org.apache.http.HttpEntity;  
import org.apache.http.HttpStatus;  
import org.apache.http.client.ClientProtocolException;  
import org.apache.http.client.methods.CloseableHttpResponse;  
import org.apache.http.client.methods.HttpGet;  
import org.apache.http.impl.client.CloseableHttpClient;  
import org.apache.http.impl.client.HttpClients;  
  
public class DownloadWebPage{  
  
    public static void downloadPagebyGetMethod() throws IOException {  
  
        // 1、通過HttpGet獲取到response對(duì)象  
        CloseableHttpClient httpClient = HttpClients.createDefault();  
        HttpGet httpGet = new HttpGet("http://www.baidu.com/");  
        CloseableHttpResponse response = httpClient.execute(httpGet);  
  
        InputStream is = null;  
        Scanner sc = null;  
        Writer os = null;  
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
            try {  
                // 2、獲取response的entity。  
                HttpEntity entity = response.getEntity();  
  
                // 3、獲取到InputStream對(duì)象,并對(duì)內(nèi)容進(jìn)行處理  
                is = entity.getContent();  
                sc = new Scanner(is);  
                // String filename = path.substring(path.lastIndexOf('/')+1);  
                String filename = "2.txt";  
                os = new PrintWriter(filename);  
                while (sc.hasNext()) {  
                    os.write(sc.nextLine());  
                }  
  
            } catch (ClientProtocolException e) {  
                e.printStackTrace();  
            } finally {  
                if (sc != null) {  
                    sc.close();  
                }  
                if (is != null) {  
                    is.close();  
                }  
                if (os != null) {  
                    os.close();  
                }  
                if (response != null) {  
                    response.close();  
                }  
            }  
        }  
  
    }  
  
    public static void main(String[] args) {  
        try {  
            downloadPagebyGetMethod();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
}  

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:javascript 時(shí)間格式輸出FormatDate函數(shù)

下一篇:Android PullToRefresh上拉和下拉刷新