c#发送get请求
2016-09-13 00:34
274 查看
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(); }
效果图
相关文章推荐
- C#模拟http 发送post或get请求
- C# 发送和接受Get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- c#发送get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- 【转】C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C# http 发送post或get请求
- 【转】C#模拟http 发送post或get请求
- EMAIL发送系统(C#+基于SMTP认证)
- C#使用CDO发送邮件
- 用.net自带的类发送E_Mail(ASP.NET+C#)
- 使用jmail组件发送电子邮件(C#)
- 对“[原创]EMAIL发送系统(C#+基于SMTP认证)”的改写
- EMAIL发送系统(C#+基于SMTP认证)
- asp.net能实现身份认证的EMAIL发送方法(c#版本)
- C#实现大文件分块发送到客户端