protected void Page_Load(object sender, EventArgs e)
              {
                  using(StreamWriter sw = new StreamWriter(Request.PhysicalApplicationPath+"index.html",false,System.Text.Encoding.GetEncoding("gb2312")))
                  {
                      sw.Write(getHtml("        }
              }
              private string getHtml(string url, string charSet)//url是要訪問的網站地址,charSet是目標網頁的編碼,如果傳入的是null或者"",那就自動分析網頁的編碼
              {

                  WebClient myWebClient = new WebClient();    //創建WebClient實例myWebClient

                  //            需要注意的:

                  //有的網頁可能下不下來,有種種原因比如需要cookie,編碼問題等等

                  //這是就要具體問題具體分析比如在頭部加入cookie

                  // webclient.Headers.Add("Cookie", cookie);

                  //這樣可能需要一些重載方法。根據需要寫就可以了

           

                  //獲取或設置用于對向 Internet 資源的請求進行身份驗證的網絡憑據。

                  myWebClient.Credentials = CredentialCache.DefaultCredentials;

                  //如果服務器要驗證用戶名,密碼

                  //NetworkCredential mycred = new NetworkCredential(struser, strpassword);

                  //myWebClient.Credentials = mycred;

                  //從資源下載數據并返回字節數組。(加@是因為網址中間有"/"符號)

                  byte[] myDataBuffer = myWebClient.DownloadData(url);

                  string strWebData = Encoding.Default.GetString(myDataBuffer);

           

                  //獲取網頁字符編碼描述信息

                  Match charSetMatch = Regex.Match(strWebData, "<meta([^<]*)charset=([^<]*)\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);

                  string webCharSet = charSetMatch.Groups[2].Value;

                  if (charSet == null || charSet == "")

                      charSet = webCharSet;

           

                  if (charSet != null && charSet != "" && Encoding.GetEncoding(charSet) != Encoding.Default)

                      strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);

                  return strWebData;

              }


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 樟树市| 四平市| 郸城县| 洛宁县| 凤冈县| 彭阳县| 江口县| 巴彦淖尔市| 新营市| 渑池县| 托里县| 邵武市| 叙永县| 武安市| 泰和县| 宜都市| 舟曲县| 娄底市| 宜兰县| 丹巴县| 台中市| 桃园县| 库尔勒市| 河西区| 肥城市| 晋城| 荃湾区| 宾阳县| 桃源县| 湖口县| 柳河县| 甘德县| 竹山县| 海城市| 靖边县| 庆元县| 东乡族自治县| 赤峰市| 德庆县| 改则县| 抚松县|