C#网页数据采集(三)HttpWebRequest

截取到网页数据是js加载完以后的

HtmlWeb webClient = new HtmlWeb();
string _url = "http://news.baidu.com/";
//需要解析的url
HtmlAgilityPack.HtmlDocument html1 = webClient.Load(_url);
//获取页面编码格式
var end3 = html1.Encoding.BodyName;
//还是需要设置一次编码格式避免乱码 调用GetHtmlSource方法
string _htmlSource = GetHtmlSource(_url, System.Text.Encoding.GetEncoding(end3));


public static string GetHtmlSource(string url, Encoding charset)
        {
            string _html = string.Empty;
            try
            {
                HttpWebRequest _request = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse _response = (HttpWebResponse)_request.GetResponse();
                using (Stream _stream = _response.GetResponseStream())
                {
                    using (StreamReader _reader = new StreamReader(_stream, charset))
                    {
                        _html = _reader.ReadToEnd();
                    }
                }
            }
            catch (WebException ex)
            {
                using (StreamReader sr = new StreamReader(ex.Response.GetResponseStream()))
                {
                    _html = sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                _html = ex.Message;
            }
            return _html;
        }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
预览图片见:http://www.cnblogs.com/xxpyeippx/archive/2008/03/31/1131211.html运行环境windows nt/xp/2003 or above.net Framework 1.1SqlServer 2000 开发环境 VS 2003目的学习了网络编程,总要做点什么东西才好。于是想到要做一个网页内容采集器。作者主页: http://www.fltek.com.cn使用方式测试数据采用自cnBlog。见下图用户首先填写“起始网页”,即从哪一页开始采集。然后填写数据库连接字符串,这里是定义了采集到的数据插入到哪个数据库,后面选择表名,不必说了。网页编码,不出意外的话,中国大陆都可以采用UTF-8爬取文件名的正则:呵呵 这个工具明显是给编程人员用的。正则都要直接填写啦。比如说cnblogs的都是数字的,所以写了\d建表帮助:用户指定要建立几个varchar型的,几个text型的,主要是放短数据和长数据啊。如果你的表里本来就有列,那就免啦。程序里面没有做验证哦。网页设置里面:采集内容前后标记:比如说都有 xxx,如果我要采集xxx就写“到”,意思,当然就是到之间的内容啦。后面的几个文本框是显示内容的。点击“获取URL”可以查看它捕获的Url对不对的。点击“采集”,可以把采集内容放到数据库,然后就用 Insert xx () (select xx) 可以直接插入目标数据了。程序代码量非常小(也非常简陋),需要的改动一下啦。不足 应用到了正则表达式、网络编程由于是最简单的东西,所以没有用多线程,没有用其他的优化方法,不支持分页。测试了一下,获取38条数据,用了700M内存啊。。。。如果有用的人 ,可以改一下使用啦。方便程序员用,免写很多代码。Surance Yin@ Surance Center 转载请注明出处
C# WinForm 应用程序中,可以使用 HttpWebRequest 类来实现文件上传,具体步骤如下: 1. 创建 HttpWebRequest 对象,并设置请求参数。 ```csharp HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uploadUrl); request.Method = "POST"; request.ContentType = "multipart/form-data"; ``` 2. 构造要上传的文件数据。 ```csharp byte[] fileData = File.ReadAllBytes(filePath); string fileName = Path.GetFileName(filePath); string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n"; string formdata = string.Format(formdataTemplate, "file", fileName); byte[] formdataBytes = Encoding.UTF8.GetBytes(formdata); byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); ``` 3. 将文件数据写入请求流中。 ```csharp Stream requestStream = request.GetRequestStream(); requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); requestStream.Write(formdataBytes, 0, formdataBytes.Length); requestStream.Write(fileData, 0, fileData.Length); byte[] trailer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); requestStream.Write(trailer, 0, trailer.Length); requestStream.Close(); ``` 4. 发送请求并获取响应。 ```csharp HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string responseString = reader.ReadToEnd(); reader.Close(); response.Close(); ``` 完整的代码示例如下: ```csharp public void UploadFile(string uploadUrl, string filePath) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uploadUrl); request.Method = "POST"; request.ContentType = "multipart/form-data"; byte[] fileData = File.ReadAllBytes(filePath); string fileName = Path.GetFileName(filePath); string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n"; string formdata = string.Format(formdataTemplate, "file", fileName); byte[] formdataBytes = Encoding.UTF8.GetBytes(formdata); byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); Stream requestStream = request.GetRequestStream(); requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); requestStream.Write(formdataBytes, 0, formdataBytes.Length); requestStream.Write(fileData, 0, fileData.Length); byte[] trailer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); requestStream.Write(trailer, 0, trailer.Length); requestStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string responseString = reader.ReadToEnd(); reader.Close(); response.Close(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值