zoukankan      html  css  js  c++  java
  • C#读取网页源码

     1     #region 1.读取 网页源码 +  static string ReadHtml(string urlStr,int type)
     2         /// <summary>
     3         /// 读取 网页源码 +  static string ReadHtml(string urlStr,int type)
     4         /// </summary>
     5         /// <param name="urlStr">网址</param>
     6         /// <param name="type">网页编码 0:utf8,1:gbk,2:gb2312</param>
     7         /// <returns>返回源码</returns>
     8         public static string ReadHtml(string urlStr,int type)
     9         {
    10             WebClient wc = new WebClient();
    11             string tempStr=string.Empty;
    12             switch (type)
    13             {
    14                 case 0:
    15                     //tempStr = Encoding.UTF8.GetString(wc.DownloadData(string.Format(urlStr)));
    16                     tempStr = Encoding.GetEncoding("utf-8").GetString(wc.DownloadData(string.Format(urlStr)));
    17                     break;
    18                 case 1:
    19                     //tempStr = Encoding.Default.GetString(wc.DownloadData(string.Format(urlStr)));
    20                     tempStr = Encoding.GetEncoding("gbk").GetString(wc.DownloadData(string.Format(urlStr)));
    21                     break;
    22                 case 2:
    23                     //tempStr = Encoding.Default.GetString(wc.DownloadData(string.Format(urlStr)));
    24                     tempStr = Encoding.GetEncoding("gb2312").GetString(wc.DownloadData(string.Format(urlStr)));
    25                     break;
    26                 default:
    27                     break;
    28             }
    29             return tempStr;
    30         }
    31         #endregion


    需要引用 using System.Net;

  • 相关阅读:
    intellij idea 将taskRequest.java文件识别为文本文档
    react 学习笔记2
    react 学习笔记1
    webpack4 配置笔记(转自掘金)
    音乐播放之进度条-自定义
    EBS
    Python 学习笔记
    Form 电子表格(JTF GRID)
    Form 中实现历史记录查询
    Form 去掉使用格式掩码带来的多余字符
  • 原文地址:https://www.cnblogs.com/iack/p/3504868.html
Copyright © 2011-2022 走看看