首页 .NET/Web C#根据url读取网站源码

C#根据url读取网站源码

作者:胡同里的砖头 围观群众:2205 更新于:2014-03-12

string GetURLContent(string url, string EncodingType)
    {
        string PetiResp = "";
        Stream mystream;
        //"http://go.microsoft.com/fwlink/?LinkId=25817"
        //"utf-8"
        System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
        req.AllowAutoRedirect = true;
        System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse)req.GetResponse();
        if (resp.StatusCode == System.Net.HttpStatusCode.OK)
        {
            mystream = resp.GetResponseStream();
            System.Text.Encoding encode = System.Text.Encoding.GetEncoding(EncodingType);
            StreamReader readStream = new StreamReader(mystream, encode);
            char[] cCont = new char[500];
            int count = readStream.Read(cCont, 0, 256);
            while (count > 0)
            {
                // Dumps the 256 characters on a string and displays the string to the console.
                String str = new String(cCont, 0, count);
                PetiResp += str;
                count = readStream.Read(cCont, 0, 256);
            }
            resp.Close();
            return PetiResp;
        }
        resp.Close();
        return null;
    }

  • 本文标题: C#根据url读取网站源码
  • 文章分类:【.NET/Web】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8