技术文章 > C#通过Url抓取网页内容

C#通过Url抓取网页内容

2018-08-19 21:14

文档管理软件,文档管理系统,知识管理系统,档案管理系统的技术资料:
[code]
csharp
using System;
using System.Text;
using System.Web;
using System.IO;
using System.Net;

class ExampleOfLegalsoft
{
public string ReadUrlContent(string rUrl)
{
// used to build entire input
StringBuilder sb = new StringBuilder();
// used on each read operation
byte[] buf = new byte[8192];
// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(rUrl);
// execute the request
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
string tempString = null;
int totalcount = 0;
int count = 0;
FileStream fs = File.Create(Server.MapPath("urltext.html"));

do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);
fs.Write(buf, 0, count);
// continue building the string
sb.Append(tempString);
}
totalcount += count;
}
while (count > 0); // any more data to read?
resStream.Close();
fs.Close();
return sb.ToString();
}
}
[/code]