asp.net中抓取远程页面,自动采集示例
2008-05-11 09:54
411 查看
由于远程网页可能采用多种编码,并且有可能采用Gzip格式来压缩数据,让我们采集页面时头疼不已,这里借鉴了一些网络上的资料,一段小程序,用来自动分析编码,自动解压缩gzip数据。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private string GetChartset(string url)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string html = getHTML(url);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Regex reg_charset = new Regex(@"charsets*=s*(?<charset>[^""]*)");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string enconding = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (reg_charset.IsMatch(html))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
enconding = reg_charset.Match(html).Groups["charset"].Value;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
enconding = Encoding.Default.EncodingName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (enconding.ToLower().Contains("gb2312"))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
enconding = "gb2312";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (enconding.ToLower().Contains("utf-8"))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
enconding = "utf-8";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return enconding;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private string getHTML(string url)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WebRequest webRequest = WebRequest.Create(url);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WebResponse webResponse = webRequest.GetResponse();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Stream stream = webResponse.GetResponseStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StreamReader sr = new StreamReader(stream, Encoding.GetEncoding(Encoding.ASCII.EncodingName));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string html = sr.ReadToEnd();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return html;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch (UriFormatException ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine(ex.Message);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch (WebException ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine(ex.Message);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private string Html(string Url)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string strResult = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string ce = response.ContentEncoding;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Stream streamReceive = response.GetResponseStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Encoding encoding = Encoding.GetEncoding(GetChartset(Url));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (ce.ToLower() == "gzip")//压缩的内容
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GZipStream gzip = new GZipStream(streamReceive, CompressionMode.Decompress);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
using (StreamReader reader = new StreamReader(gzip,encoding))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
strResult = reader.ReadToEnd();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
catch (Exception ex) ...{ HttpContext.Current.Response.Write(ex.ToString()); }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return strResult;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected void Button1_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Write(Html(TextBox1.Text));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
使用起来很简单,下面的程序将字符串压缩入文件:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
using (DeflateStream gzip = new DeflateStream(fs, CompressionMode.Compress))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] buf = Encoding.UTF8.GetBytes(this.txbSource.Text);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
gzip.Write(buf, 0, buf.Length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
gzip.Flush();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
解压只需要这样:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
gzip = new GZipStream(new MemoryStream(buf), CompressionMode.Decompress);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
using (StreamReader reader = new StreamReader(gzip))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txbTarget.Text = reader.ReadToEnd();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
如果从文件解压,只需要把MemoryStream换成一个FileStream就行了。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private string GetChartset(string url)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string html = getHTML(url);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Regex reg_charset = new Regex(@"charsets*=s*(?<charset>[^""]*)");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string enconding = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (reg_charset.IsMatch(html))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
enconding = reg_charset.Match(html).Groups["charset"].Value;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
enconding = Encoding.Default.EncodingName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (enconding.ToLower().Contains("gb2312"))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
enconding = "gb2312";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (enconding.ToLower().Contains("utf-8"))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
enconding = "utf-8";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return enconding;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private string getHTML(string url)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WebRequest webRequest = WebRequest.Create(url);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WebResponse webResponse = webRequest.GetResponse();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Stream stream = webResponse.GetResponseStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StreamReader sr = new StreamReader(stream, Encoding.GetEncoding(Encoding.ASCII.EncodingName));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string html = sr.ReadToEnd();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return html;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch (UriFormatException ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine(ex.Message);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch (WebException ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine(ex.Message);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private string Html(string Url)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string strResult = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string ce = response.ContentEncoding;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Stream streamReceive = response.GetResponseStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Encoding encoding = Encoding.GetEncoding(GetChartset(Url));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (ce.ToLower() == "gzip")//压缩的内容
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GZipStream gzip = new GZipStream(streamReceive, CompressionMode.Decompress);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
using (StreamReader reader = new StreamReader(gzip,encoding))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
strResult = reader.ReadToEnd();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
catch (Exception ex) ...{ HttpContext.Current.Response.Write(ex.ToString()); }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return strResult;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected void Button1_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Write(Html(TextBox1.Text));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
使用起来很简单,下面的程序将字符串压缩入文件:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
using (DeflateStream gzip = new DeflateStream(fs, CompressionMode.Compress))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] buf = Encoding.UTF8.GetBytes(this.txbSource.Text);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
gzip.Write(buf, 0, buf.Length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
gzip.Flush();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
解压只需要这样:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
gzip = new GZipStream(new MemoryStream(buf), CompressionMode.Decompress);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
using (StreamReader reader = new StreamReader(gzip))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txbTarget.Text = reader.ReadToEnd();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
如果从文件解压,只需要把MemoryStream换成一个FileStream就行了。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
相关文章推荐
- Asp.net 数据采集基类(远程抓取,分解,保存,匹配)
- Asp.net 数据采集(远程抓取,小偷程序) 基类
- Asp.net 数据采集基类(远程抓取,分解,保存,匹配)
- [转] Asp.net 数据采集基类(远程抓取,分解,保存,匹配)
- Asp.net 数据采集基类(远程抓取,分解,保存,匹配)
- asp.net 获取远程页面 示例
- Asp.net 数据采集基类(远程抓取,分解,保存,匹配)
- Asp.net 数据采集基类(远程抓取,分解,保存,匹配)
- Asp.net 数据采集基类(远程抓取,分解,保存,匹配)
- Asp.net 数据采集基类(远程抓取,分解,保存,匹配)
- [转]Asp.net 数据采集基类(远程抓取,分解,保存,匹配)
- asp.net网站首页根据IP自动跳转指定页面的示例
- asp.net网站首页根据IP自动跳转指定页面的示例
- [转] Asp.net 数据采集基类(远程抓取,分解,保存,匹配)
- Asp.net 数据采集基类(远程抓取,分解,保存,匹配)
- 【转】 asp.net从视频文件中抓取一桢并生成图像文件的方法 实现多语言本地化应用程序 自动返回上次请求页面
- Asp.net 数据采集基类(远程抓取,分解,保存,匹配)
- Asp.net 数据采集基类(远程抓取,分解,保存,匹配)(转)
- Asp.net 数据采集基类(远程抓取,分解,保存,匹配)
- Asp.net 数据采集基类(远程抓取,分解,保存,匹配)