c#发送get请求

版权声明:本文为博主原创文章,未经博主允许转载随意。 https://blog.csdn.net/kebi007/article/details/52519086

c#发送get请求爬取网页

关键点:在控制台中发送一个get请求,将响应的内容写入文件流中保存html格式
        static void Main(string[] args)
        {
            string url = "";
            HttpWebResponse response = CreateGetHttpResponse(url);
            //获取流
            Stream streamResponse = response.GetResponseStream();
            //使用UTF8解码
            StreamReader streanReader = new StreamReader(streamResponse,Encoding.UTF8);

            string retString = streanReader.ReadToEnd();
            //先打印内容
            Console.WriteLine(retString);
            //保存文件
            SaveFile(retString);


        }
        //创建GET的HTTP请求
        public static HttpWebResponse CreateGetHttpResponse(string url)
        {
            HttpWebRequest request =(HttpWebRequest) WebRequest.Create(url);
            request.ContentType = "text/html;chartset=UTF-8";
            request.UserAgent = "Mozilla / 5.0(Windows NT 10.0; Win64; x64; rv: 48.0) Gecko / 20100101 Firefox / 48.0"; //火狐用户代理
            request.Method = "GET";
            return (HttpWebResponse)request.GetResponse();
        }
        //保存文件
        public static void SaveFile(string  content)
        {
            string dirPath = @"D:\test";

            string filePath = dirPath + @"\getRequest.html"; 
            //先创建保存的路径
            if (!Directory.Exists(dirPath))
            Directory.CreateDirectory(dirPath);
            if (!Directory.Exists(filePath))
            using (File.Create(filePath)) ;

            //创建文件流
            FileStream fs = new FileStream(filePath,FileMode.Create,FileAccess.Write);
            //创建写入器
            StreamWriter sr = new StreamWriter(fs);
            sr.Write(content);
            sr.Close();
            fs.Close();
        }
    

效果图

没有更多推荐了,返回首页