Java Tools

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            83 隨筆 :: 0 文章 :: 16 評論 :: 0 Trackbacks
          引言
            在做無線項目的時候,與通訊公司的數據通訊有一部分是通過XML交互的,所以必須要動態抓取通訊公司提供的固定的Internet上的數據,便研究了一下如何抓取固定url上的數據,現與大家分享一下。

            類名GetPageCode,有一個方法GetSource,通過屬性傳遞參數,入參控制的是要取得URL的地址,代理服務器的設置及輸出方式的控制,這里大家可以再擴展自己的需要,我這里只提供了兩種方式,一種是直接寫到本地的某個文件中,另外一種就是返回字符串的。類里已經作了比較詳細的注釋,我想大家很容易就看明白了,如果實在不明白, 那就msn上問吧,MSN:yubo@x263.net。

            調用方式:
            #region 測試獲取遠程網頁



          GetPageCode gpc = new GetPageCode();
            gpc.Url="http://ppcode.com";
            gpc.ProxyState=1;//使用代理服務器,0為不使用,設置為1后下面的代理設置才起作用
            gpc.ProxyAddress="http://proxyName.com";//代理服務器地址
            gpc.ProxyPort="80";//代理服務器的端口
            gpc.ProxyAccount="proxy";//代理服務器賬號
            gpc.ProxyPassword="password";//代理服務器密碼
            gpc.ProxyDomain="bqc";//代理服務器域
            gpc.OutFilePath=filePath;//設置輸出文件路徑的地方,如果不設置,則返回字符串
            gpc.GetSource();//處理
            string tempErr=gpc.NoteMessage;//如果出錯,這里會提示
            string tempCode=gpc.OutString;//返回的字符串
            #endregion
            類代碼:
            using System;
            using System.Collections;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.IO;
            using System.Net;
            using System.Text;
            using System.Web;
            namespace Test.Com
            {
             /// <summary>
             /// 功能:取得Internet上的URL頁的源碼
             /// 創建:2004-03-22
             /// 作者:Rexsp MSN:yubo@x263.net
            /// </summary>
             public class GetPageCode
             {
             #region 私有變量
            /// <summary>
            /// 網頁URL地址
            /// </summary>
            private string url=null;
            /// <summary>
            /// 是否使用代碼服務器:0 不使用  1 使用代理服務器
            /// </summary>
            private int proxyState=0;
            /// <summary>
            /// 代理服務器地址
            /// </summary>
            private string proxyAddress=null;
            /// <summary>
            /// 代理服務器端口
            /// </summary>
            private string proxyPort=null;
            /// <summary>
            /// 代理服務器用戶名
            /// </summary>
            private string proxyAccount=null;
            /// <summary>
            /// 代理服務器密碼
            /// </summary>
            private string proxyPassword=null;
            /// <summary>
            /// 代理服務器域
            /// </summary>
            private string proxyDomain=null;
           /// <summary>
            /// 輸出文件路徑
            /// </summary>
            private string outFilePath=null;
            /// <summary>
            /// 輸出的字符串
            /// </summary>
            private string outString=null;
            /// <summary>
            /// 提示信息
            /// </summary>
            private string noteMessage;

            #endregion

            #region 公共屬性
            /// <summary>
            /// 欲讀取的URL地址
            /// </summary>
            public string Url
            {
             get{return url;}
             set{url=value;}
            }
            /// <summary>
            /// 是否使用代理服務器標志
            /// </summary>
            public int ProxyState
            {
             get{return proxyState;}
             set{proxyState=value;}
            }
            /// <summary>
            /// 代理服務器地址
            /// </summary>
            public string ProxyAddress
            {
             get{return proxyAddress;}
             set{proxyAddress=value;}
            }
            /// <summary>

            /// 代理服務器端口
            /// </summary>
            public string ProxyPort
            {
             get{return proxyPort;}
             set{proxyPort=value;}
            }
            /// <summary>
            /// 代理服務器賬號
            /// </summary>
            public string ProxyAccount
            {
             get{return proxyAccount;}
             set{proxyAccount=value;}
            }
            /// <summary>
            /// 代理服務器密碼
            /// </summary>
            public string ProxyPassword
            {
             get{return proxyPassword;}
             set{proxyPassword=value;}
            }
            /// <summary>
            /// 代理服務器域
            /// </summary>
            public string ProxyDomain
            {
             get{return proxyDomain;}
             set{proxyDomain=value;}
            }
            /// <summary>
            /// 輸出文件路徑
            /// </summary>
            public string OutFilePath
            {
             get{return outFilePath;}

            set{outFilePath=value;}
            }
            /// <summary>
            /// 返回的字符串
            /// </summary>
            public string OutString
            {
             get{return outString;}
             
            }
            /// <summary>
            /// 返回提示信息
            /// </summary>
            public string NoteMessage
            {
             get{return noteMessage;}
             
            }
            
            #endregion
            
            #region 構造函數
            public GetPageCode()
            {
            }
            #endregion

            #region 公共方法
            /// <summary>
            /// 讀取指定URL地址,存到指定文件中
            /// </summary>
            public void GetSource() 
            { 
             WebRequest request = WebRequest.Create(this.url);
             //使用代理服務器的處理
             if(this.proxyState==1)
             {
              //默認讀取80端口的數據

              if(this.proxyPort==null)
               this.ProxyPort="80";

              WebProxy myProxy=new WebProxy(); 
              myProxy = (WebProxy)request.Proxy; 
              myProxy.Address = new Uri(this.ProxyAddress+":"+this.ProxyPort); 
              myProxy.Credentials = new NetworkCredential(this.proxyAccount, this.proxyPassword, this.ProxyDomain);
              request.Proxy = myProxy; 
             }
             try
             
             {
              //請求服務
              WebResponse response = request.GetResponse();
              //返回信息
              Stream resStream = response.GetResponseStream(); 
              StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
              string tempCode= sr.ReadToEnd();
              resStream.Close(); 
              sr.Close();

              //如果輸出文件路徑為空,便將得到的內容賦給OutString屬性
              if(this.outFilePath==null)
              {
               this.outString=tempCode;
              }
              else
              {

               FileInfo fi = new FileInfo(this.outFilePath);
               //如果存在文件則先干掉
               if(fi.Exists)
                fi.Delete();
               StreamWriter sw = new StreamWriter(this.outFilePath,true,Encoding.Default);
               sw.Write(tempCode);
               sw.Flush();
               sw.Close();
              }
             }
             catch
             {
              this.noteMessage="出錯了,請檢查網絡是否連通;";
               }

                }
             #endregion

             }
            }
          posted on 2007-07-03 14:52 和田雨 閱讀(259) 評論(0)  編輯  收藏 所屬分類: J2SE案例
          主站蜘蛛池模板: 邵阳市| 赤城县| 中阳县| 吉木乃县| 吴堡县| 石楼县| 革吉县| 全南县| 洛南县| 太仆寺旗| 筠连县| 中宁县| 河北区| 会宁县| 环江| 临西县| 皋兰县| 高要市| 八宿县| 开平市| 宁阳县| 玉林市| 阜阳市| 双城市| 山阴县| 扎赉特旗| 北碚区| 宜丰县| 海淀区| 云浮市| 宜章县| 常熟市| 五家渠市| 南丹县| 腾冲县| 武乡县| 蒙城县| 灌阳县| 稷山县| 通州区| 武邑县|