下載網頁的基本方法

來源:互聯網
上載者:User

標籤:

一、Java.net.URL

 1 import java.io.BufferedReader; 2 import java.io.InputStreamReader; 3 import java.net.URL; 4  5 public class RetrivePage { 6     public static String downloadPage(String path) throws Exception { 7         URL pageURL = new URL(path); 8         BufferedReader reader = new BufferedReader(new InputStreamReader(pageURL.openStream())); 9         String line;10         StringBuilder pageBuffer = new StringBuilder();11         while ((line = reader.readLine()) != null) {12             pageBuffer.append(line);13         }14         return pageBuffer.toString();15     }16 17     public static void main(String args[]) throws Exception {18         System.out.println(RetrivePage.downloadPage("http://www.sina.com"));19     }20 }

二、Scanner對象

 1 import java.io.InputStreamReader; 2 import java.net.URL; 3 import java.util.Scanner; 4  5 public class RetrivePage { 6     public static String downloadPage(String path) throws Exception { 7         URL pageURL = new URL(path); 8         Scanner scanner = new Scanner(new InputStreamReader(pageURL.openStream(), "utf-8")); 9         scanner.useDelimiter("\\z");10         StringBuilder pageBuffer = new StringBuilder();11         while (scanner.hasNext()) {12            pageBuffer.append(scanner.next());13         }14         return pageBuffer.toString();15     }16 17     public static void main(String args[]) throws Exception {18         System.out.println(RetrivePage.downloadPage("http://www.sina.com"));19     }20 }

三、通訊端

 1 import java.io.*; 2 import java.net.Socket; 3  4 public class RetrivePage { 5     public static void main(String args[]) throws Exception { 6         String host = "blog.csdn.net"; 7         String file = "/column.html"; 8         int port = 80; 9         Socket s = new Socket(host, port);10         OutputStream out = s.getOutputStream();11         PrintWriter outw = new PrintWriter(out, false);12         outw.print("GET" + file + " HTTP/1.0\r\n");13         outw.print("Accept:text/plain,text/html,text/*\r\n");14         outw.print("\r\n");15         outw.flush();16         InputStream in = s.getInputStream();17         InputStreamReader inr = new InputStreamReader(in);18         BufferedReader bufferedReader = new BufferedReader(inr);19         String line;20         while ((line = bufferedReader.readLine()) != null) {21             System.out.println(line);22         }23     }24 }

四、HttpClient

 1 import org.apache.http.HttpEntity; 2 import org.apache.http.HttpResponse; 3 import org.apache.http.client.HttpClient; 4 import org.apache.http.client.methods.HttpGet; 5 import org.apache.http.impl.client.DefaultHttpClient; 6 import org.apache.http.util.EntityUtils; 7 public class RetrivePage { 8     public static void main(String args[]) throws Exception { 9         HttpClient httpClient=new DefaultHttpClient();10         HttpGet httpGet=new HttpGet("http://www.sina.com");11         HttpResponse response=httpClient.execute(httpGet);12         HttpEntity entity=response.getEntity();13         if(entity!=null){14             System.out.println(EntityUtils.toString(entity,"utf-8"));15             EntityUtils.consume(entity);16         }17         httpClient.getConnectionManager().shutdown();18     }19 }

 

下載網頁的基本方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.