返回
顶部

修改密码

首页 > 教程 > 网络通信 > HTTP > 正文
android怎样获取网站html源文件

+1

-1

收藏

+1

-1

点赞0

评论0

import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import junit.framework.TestCase; import org.junit.Test; public…
 
 
  1. import java.io.ByteArrayOutputStream;  
  2. import java.io.File;  
  3. import java.io.FileOutputStream;  
  4. import java.io.InputStream;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URL;  
  7.   
  8. import junit.framework.TestCase;  
  9.   
  10. import org.junit.Test;  
  11.   
  12.   
  13. public class TestInternet extends TestCase  
  14. {  
  15.     public byte[] readStream(InputStream inputStream) throws Exception  
  16.     {  
  17.         byte[] buffer=new byte[1024];  
  18.         int len=-1;  
  19.         ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();  
  20.           
  21.         while((len=inputStream.read(buffer))!=-1)  
  22.         {  
  23.             byteArrayOutputStream.write(buffer,0,len);  
  24.         }  
  25.           
  26.         inputStream.close();  
  27.         byteArrayOutputStream.close();  
  28.         return byteArrayOutputStream.toByteArray();  
  29.     }  
  30.     /** 
  31.      * 获取网址的html 
  32.      * @throws Exception 
  33.      */  
  34.     @Test public void testGetHtml() throws Exception  
  35.     {  
  36.         String urlpath="http://www.sina.com.cn/";  
  37.         URL url=new URL(urlpath);  
  38.         HttpURLConnection conn=(HttpURLConnection)url.openConnection();  
  39.         conn.setConnectTimeout(6*1000);  //设置链接超时时间6s  
  40.             //在android系统中,如果超过组件的阻塞时间,组件会被系统回收。时间大约10s。  
  41.         conn.setRequestMethod("GET");  
  42.   
  43.         if(conn.getResponseCode()==200)  
  44.         {  
  45.             InputStream inputStream=conn.getInputStream();  
  46.             byte[] data=readStream(inputStream);  
  47.             System.out.println(new String(data));  
  48.         }  
  49.     }  
  50.       
  51.     /** 
  52.      * 获取网上图片 
  53.      * @throws Exception 
  54.      */  
  55.     @Test public void testGetImage() throws Exception  
  56.     {  
  57.         String urlpath="http://h1.qhimg.com/images/logo/search-logo.png";//网上图片的地址  
  58.         URL url=new URL(urlpath);  
  59.         HttpURLConnection conn=(HttpURLConnection)url.openConnection();  
  60.         conn.setConnectTimeout(6*1000);  //设置链接超时时间6s  
  61.             //在android系统中,如果超过组件的阻塞时间,组件会被系统回收。时间大约10s。  
  62.         conn.setRequestMethod("GET");  
  63.         System.out.println(conn.getResponseCode());  
  64.         if(conn.getResponseCode()==200)  
  65.         {  
  66.             InputStream inputStream=conn.getInputStream();  
  67.             byte[] data=readStream(inputStream);  
  68.             File file=new File("logo.png");  
  69.             FileOutputStream fileOutputStream=new FileOutputStream(file);  
  70.             fileOutputStream.write(data);  
  71.             fileOutputStream.close();  
  72.         }  
  73.     }  
  74. }  


 假如我要获取网站的html用于我的手机开发,例如:

 

 

 

 

在控制台我们可以看到:

 

 

即我们成功获取。

扫一扫在手机打开

评论
已有0条评论
0/150
提交
热门评论
相关推荐
今日要闻
换一批
热点排行