中易网

求HTTP C# 下载类

答案:3  悬赏:70  
解决时间 2021-02-10 11:30
求HTTP C# 下载类
最佳答案
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;

namespace WebGatherClass
{
///
/// 采集方法集合
///

public class WebGatherFunction
{
///
/// 获取网页html信息
/// 如果errStr != String.Empty,则表示获取错误。
///

/// 网址
/// Post数据
/// 错误信息返回
///
public static string GetPostPageHtml(string url, string postData, out string errStr)
{
errStr = string.Empty;
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.Default;
byte[] data = encoding.GetBytes(postData);

try
{
// 设置参数
request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
outstream = request.GetRequestStream();
outstream.Write(data, 0, data.Length);
outstream.Close();

//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;

//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);

//返回结果网页(html)代码
string content = sr.ReadToEnd();
return content.ToLower();
}
catch (Exception ex)
{
errStr = ex.Message;
return null;
}
finally
{
if (outstream != null)
{
outstream.Close();
}
if (instream != null)
{
instream.Close();
}
if (sr != null)
{
sr.Close();
}
if (response != null)
{
response.Close();
}
if (request != null)
{
request = null;
}
}
}

///
/// 获取网页html信息
/// 如果errStr != String.Empty,则表示获取错误。
///

/// 网址
/// 错误信息返回
/// 网页编码
///
public static string GetPageHtml(string url, string encodingStr, out string errStr)
{
errStr = string.Empty;
WebRequest myReq = null;
WebResponse webResponse = null;
try
{
myReq = WebRequest.Create(url);

webResponse = myReq.GetResponse();
}
catch (Exception ex)
{
errStr = ex.Message;
return null;
}

String charset = null;
Encoding e = null;
if (charset == null)
{
//如果发现content-type头
String ctype = webResponse.Headers["content-type"];
if (ctype != null)
{
int ind = ctype.IndexOf("charset=");
if (ind != -1)
{
charset = ctype.Substring(ind + 8);
}
}
}

Stream resStream = null;
StreamReader sr = null;
try
{
resStream = webResponse.GetResponseStream();

MemoryStream rawdata = new MemoryStream();
byte[] buffer = new byte[1024];
int read = resStream.Read(buffer, 0, buffer.Length);
while (read > 0)
{
rawdata.Write(buffer, 0, read);
read = resStream.Read(buffer, 0, buffer.Length);
}

//如果没有发现content-type,只好从脚本中搜索了
if (charset == null)
{
rawdata.Seek(0, SeekOrigin.Begin);

StreamReader srr = new StreamReader(rawdata, Encoding.ASCII);
String meta = srr.ReadToEnd().ToLower();

if (meta != null)
{
int start_ind = meta.IndexOf("charset=");
int end_ind = -1;
if (start_ind != -1)
{
end_ind = meta.IndexOf("\"", start_ind);
if (end_ind != -1)
{
int start = start_ind + 8;
charset = meta.Substring(start, end_ind - start + 1);
charset = charset.TrimEnd(new Char[] { '>', '"' }).Trim();
}
}

if (charset == null)
{
start_ind = meta.IndexOf("encoding=");
end_ind = -1;
if (start_ind != -1)
{
end_ind = meta.IndexOf("?", start_ind);
if (end_ind != -1)
{
int start = start_ind + 9;
charset = meta.Substring(start, end_ind - start);
charset = charset.Trim(new Char[] { '\'', '"' }).Trim();

}
}
}
}
}

if (charset == null)
{
try
{
e = Encoding.GetEncoding(encodingStr);
}
catch
{
e = Encoding.UTF8;
}
}
else if (e == null)
{
try
{
e = Encoding.GetEncoding(charset);
}
catch
{
try
{
e = Encoding.GetEncoding(encodingStr);
}
catch
{
e = Encoding.UTF8;
}
}
}

rawdata.Seek(0, SeekOrigin.Begin);
sr = new StreamReader(rawdata, e);
string htmlStr = sr.ReadToEnd();

rawdata.Close();
resStream.Close();
webResponse.Close();

return htmlStr;
}
catch (Exception ex)
{
errStr = ex.Message;
return null;
}
finally
{
webResponse.Close();
if (resStream != null)
{
resStream.Close();
}
if (sr != null)
{
sr.Close();
}
}
}

///
/// 获取网页html信息
/// 如果errStr != String.Empty,则表示获取错误。
///

/// 网址
/// 编码格式
/// 错误信息返回
///
public static string GetPageHtmlByEncoding(string url, string encodingStr, out string errStr)
{
errStr = string.Empty;

Stream resStream = null;
StreamReader sr = null;
try
{
WebRequest myReq = WebRequest.Create(url);

using (WebResponse webResponse = myReq.GetResponse())
{
String charset = encodingStr;
resStream = webResponse.GetResponseStream();

MemoryStream rawdata = new MemoryStream();
byte[] buffer = new byte[1024];
int read = resStream.Read(buffer, 0, buffer.Length);
while (read > 0)
{
rawdata.Write(buffer, 0, read);
read = resStream.Read(buffer, 0, buffer.Length);
}

Encoding encoding = null;

try
{
encoding = Encoding.GetEncoding(charset);
}
catch
{
encoding = Encoding.UTF8;
}

rawdata.Seek(0, SeekOrigin.Begin);
sr = new StreamReader(rawdata, encoding);
string htmlStr = sr.ReadToEnd();

rawdata.Close();
resStream.Close();
webResponse.Close();

return htmlStr;
}
}
catch (Exception ex)
{
errStr = ex.Message;
return null;
}
finally
{
if (resStream != null)
{
resStream.Close();
}
if (sr != null)
{
sr.Close();
}
}

}

///
/// 下载图片到本地目录
///

///
///
///
internal static bool RequestWebImg(string imgUrl, string imgSavePath)
{
try
{
WebClient client = new WebClient();

client.DownloadFile(imgUrl, imgSavePath);
return true;
}
catch
{
return false;
}
}

///
/// 下载图片到本地目录
///

///
///
///
internal static bool DownLoadWebImg(string imgUrl, string imgSavePath)
{
HttpWebRequest myReq = null;
HttpWebResponse webResponse = null;
try
{
myReq = (HttpWebRequest)HttpWebRequest.Create(imgUrl);
myReq.AllowAutoRedirect = true;
webResponse = (HttpWebResponse)myReq.GetResponse();
}
catch
{
return false;
}

int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int readBytes = 0;
try
{
using (FileStream stream = new FileStream(imgSavePath, FileMode.Append))
{
Stream sr = null;
try
{
sr = webResponse.GetResponseStream();
do
{
readBytes = sr.Read(buffer, 0, bufferSize);
stream.Write(buffer, 0, readBytes);
}
while (readBytes != 0);
}
catch
{
return false;
}
finally
{
try
{
sr.Close();
stream.Close();
}
catch
{
}
}
return true;
}
}
catch
{
return false;
}
}
}
}
全部回答
有了就不贴了@
WebClient也行啊
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
松鼠吃我家种的菜,怎么办?
园林景观硬化重要还是绿化重要
嘛尼石体验馆地址在什么地方,想过去办事
信隆平价超市怎么去啊,有知道地址的么
有做盐焗鹌鹑蛋的吗
逸凡阳光主题公寓(深圳罗湖红岭地铁店)地址
有没有职业是做警察的,如果你碰到一个未成年
win7怎么样提高电脑的亮度
请问fornarina这个牌子怎么样???网上有没
向阳社区(三新东路)在哪里啊,我有事要去这个
长方体是不是立体图形
陕西电动车下乡中标品牌
中国工商银行非晶片卡还能用吗?
中控(ZKSoftware) 指纹考勤机,长按M/OK键会
沙口社区卫生站我想知道这个在什么地方
推荐资讯
豆角月亮的含义是什么?
苏州吴中区商铺税费怎么算?
竹溪名扬工贸有限公司怎么去啊,有知道地址的
无线路由器摆在头顶危害大不大
90后和00后,都喜欢什么东西!?
萨米特陶瓷怎么去啊,有知道地址的么
咸丰路街道在哪里啊,我有事要去这个地方
盘锦华源橡塑有限公司这个地址在什么地方,我
DNF安图恩团队模式黑雾吞噬魔怎么打,我的武
钢筋调直机编码器五根线怎么接四根线
大连西安路的长兴电子城怎么样?电脑是正品吗
怎么取消移动给我发彩信的那个业务
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?