c#获取网页源码全解

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.Net;
using System.IO;

namespace util
{
    public class webcode
    {

        //获取网页源代码
        private string getHtml(string url)
        {
            WebClient myWebClient = new WebClient();
            byte[] myDataBuffer = myWebClient.DownloadData(url);
            return Encoding.Default.GetString(myDataBuffer);
        }

        //public bool getweb(string strURL, out string buf)
        //{
        //    buf = "";
        //    try
        //    {
        //        //Uri url=new Uri(strURL,false);
        //        HttpWebRequest request;
        //        request = (HttpWebRequest)WebRequest.Create(strURL);
        //        request.Method = "POST"; //Post请求方式
        //        request.ContentType = "text/html;charset=gb2312"; //内容类型
        //        string paraUrlCoded = System.Web.HttpUtility.UrlEncode(""); //参数经过URL编码
        //        byte[] payload;
        //        payload = System.Text.Encoding.GetEncoding("GB2312").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节
        //        request.ContentLength = payload.Length; //设置请求的ContentLength
        //        Stream writer = request.GetRequestStream(); //获得请求流
        //        writer.Write(payload, 0, payload.Length); //将请求参数写入流
        //        writer.Close(); //关闭请求流
        //        HttpWebResponse response;
        //        response = (HttpWebResponse)request.GetResponse(); //获得响应流
        //        Stream s;
        //        s = response.GetResponseStream();
        //        StreamReader objReader = new StreamReader(s, System.Text.Encoding.GetEncoding("GB2312"));
        //        string HTML = "";
        //        string sLine = "";
        //        int i = 0;
        //        while (sLine != null)
        //        {
        //            i++;
        //            sLine = objReader.ReadLine();
        //            if (sLine != null)
        //                HTML += sLine;
        //        }
        //        //HTML = HTML.Replace("&lt;","<");
        //        //HTML = HTML.Replace("&gt;",">");
        //        buf = HTML;
        //        return true;
        //    }
        //    catch (Exception x)
        //    {
        //        buf = x.Message.ToString();
        //        return false;
        //    }
        //}

        //带Cookie:
        CookieContainer cc = new CookieContainer();
        public bool getweb(string strURL,out string buf)
        {
            buf="";
            try
            {
                HttpWebRequest request;
                request = (HttpWebRequest)WebRequest.Create(strURL);
                request.Method="POST"; //Post请求方式
                request.ContentType="text/html;charset=gb2312"; //内容类型
                string paraUrlCoded = System.Web.HttpUtility.UrlEncode(""); //参数经过URL编码
                byte[] payload;
                payload = System.Text.Encoding.GetEncoding("GB2312").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节
                request.ContentLength = payload.Length; //设置请求的ContentLength
                Stream writer = request.GetRequestStream(); //获得请求流
                writer.Write(payload,0,payload.Length); //将请求参数写入流
                writer.Close(); //关闭请求流
                HttpWebResponse response;
                response = (HttpWebResponse)request.GetResponse(); //获得响应流
                Stream s;
                s = response.GetResponseStream();
                StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
                string HTML = "";
                string sLine ="";
                int i = 0;
                while (sLine!=null)
                {
                     i++;
                     sLine = objReader.ReadLine();
                     if (sLine!=null)
                      HTML += sLine;
                } 
                buf=HTML;
                return true;
            }
            catch (Exception x)
            {   
                buf=x.Message.ToString();
                return false;    
            }
        }

        public bool getweb(string strURL, out string buf, string postData)
        {
            buf = "";
            try
            {
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] data = encoding.GetBytes(postData);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                Stream newStream = request.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();

                request.CookieContainer = cc;

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                cc.Add(response.Cookies);
                Stream stream = response.GetResponseStream();
                string sHtml = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
                buf = sHtml;
                return true;
            }
            catch (Exception x)
            {
                buf = x.Message.ToString();
                return false;
            }
        }

        private string getWebresourceFile1(string url)
        {
            WebClient myWebClient = new WebClient();
            byte[] myDataBuffer = myWebClient.DownloadData(url);
            string SourceCode = Encoding.Default.GetString(myDataBuffer);
            return SourceCode;
        }

        private string getWebresourceFile2(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            request.Method = "GET ";
            Stream receiveStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader(receiveStream, Encoding.Default);
            string SourceCode = readStream.ReadToEnd();
            response.Close();
            readStream.Close();
            return SourceCode;
        }


        private string getWebresourceFile3(string url)
        {
            WebClient wc = new WebClient();
            wc.Credentials = CredentialCache.DefaultCredentials;
            Byte[] pageData = wc.DownloadData(url);
            string SourceCode = Encoding.Default.GetString(pageData);
            wc.Dispose();
            return SourceCode;
        }


        private string getWebresourceFile4(string url)
        {
            WebClient wc = new WebClient();
            wc.Credentials = CredentialCache.DefaultCredentials;
            Stream resStream = wc.OpenRead(url);
            StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
            string SourceCode = sr.ReadToEnd();
            resStream.Close();
            wc.Dispose();
            return SourceCode;
        }


        private string getWebresourceFile5(string url)
        {
            WebRequest request = WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            Stream resStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
            string SourceCode = sr.ReadToEnd();
            resStream.Close();
            sr.Close();
            return SourceCode;
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

腾讯AI架构师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值