java 獲取網頁內容

java  string  url  exception  server  null java

權限:android

<uses-permission android:name=「android.permission.INTERNET」></uses-permission>web

注意:訪問網絡必須放線程裏網絡

[java]           view plain     copy    app

<EMBED id=ZeroClipboardMovie_1 height=18 name=ZeroClipboardMovie_1 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=1&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">oop

  1. package test;  url

  2.   

  3.   

  4.   

  5. import java.io.BufferedReader;  spa

  6.   

  7. import java.io.IOException;  .net

  8.   

  9. import java.io.InputStream;  線程

  10.   

  11. import java.io.InputStreamReader;  

  12.   

  13. import java.net.Authenticator;  

  14.   

  15. import java.net.HttpURLConnection;  

  16.   

  17. import java.net.PasswordAuthentication;  

  18.   

  19. import java.net.URL;  

  20.   

  21. import java.net.URLConnection;  

  22.   

  23. import java.util.Properties;  

  24.   

  25.   

  26.   

  27. public class URLTest {  

  28.   

  29.     // 一個public方法,返回字符串,錯誤則返回"error open url"  

  30.   

  31.     public static String getContent(String strUrl) {  

  32.   

  33.         try {  

  34.   

  35.             URL url = new URL(strUrl);  

  36.   

  37.             BufferedReader br = new BufferedReader(new InputStreamReader(url  

  38.   

  39.                     .openStream()));  

  40.   

  41.             String s = "";  

  42.   

  43.             StringBuffer sb = new StringBuffer("");  

  44.   

  45.             while ((s = br.readLine()) != null) {  

  46.   

  47.                 sb.append(s + "/r/n");  

  48.   

  49.             }  

  50.   

  51.             br.close();  

  52.   

  53.             return sb.toString();  

  54.   

  55.         } catch (Exception e) {  

  56.   

  57.             return "error open url:" + strUrl;  

  58.   

  59.         }  

  60.   

  61.     }  

  62.   

  63.   

  64.   

  65.     public static void initProxy(String host, int port, final String username,  

  66.   

  67.             final String password) {  

  68.   

  69.         Authenticator.setDefault(new Authenticator() {  

  70.   

  71.             protected PasswordAuthentication getPasswordAuthentication() {  

  72.   

  73.                 return new PasswordAuthentication(username,  

  74.   

  75.                         new String(password).toCharArray());  

  76.   

  77.             }  

  78.   

  79.         });  

  80.   

  81.         System.setProperty("http.proxyType""4");  

  82.   

  83.         System.setProperty("http.proxyPort", Integer.toString(port));  

  84.   

  85.         System.setProperty("http.proxyHost", host);  

  86.   

  87.         System.setProperty("http.proxySet""true");  

  88.   

  89.     }  

  90.   

  91.   

  92.   

  93.     public static void main(String[] args) throws IOException {  

  94.   

  95.          String url = "http://java.sun.com";  

  96.   

  97.          String proxy = "http://192.168.22.81";  

  98.   

  99.          int port = 80;  

  100.   

  101.          String username = "username";  

  102.   

  103.          String password = "password";  

  104.   

  105.          String curLine = "";  

  106.   

  107.          String content = "";  

  108.   

  109.          URL server = new URL(url);  

  110.   

  111.          initProxy(proxy, port, username, password);  

  112.   

  113.          HttpURLConnection connection = (HttpURLConnection) server  

  114.   

  115.          .openConnection();  

  116.   

  117.          connection.connect();  

  118.   

  119.          InputStream is = connection.getInputStream();  

  120.   

  121.          BufferedReader reader = new BufferedReader(new  

  122.   

  123.          InputStreamReader(is));  

  124.   

  125.          while ((curLine = reader.readLine()) != null) {  

  126.   

  127.          content = content + curLine+ "/r/n";  

  128.   

  129.          }  

  130.   

  131.          System.out.println("content= " + content);  

  132.   

  133.          is.close();  

  134.   

  135.          System.out.println(getContent(url));  

  136.   

  137.     }  

  138.   

  139. }  

         

相關文章
相關標籤/搜索