求HTTP C# 下载类
答案:3 悬赏:70
解决时间 2021-02-10 11:30
- 提问者网友:妖精ξ也會哭
- 2021-02-10 04:00
求HTTP C# 下载类
最佳答案
- 二级知识专家网友:初心未变
- 2021-02-10 05:17
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;
}
}
}
}
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;
}
}
}
}
全部回答
- 1楼网友:冷眼_看世界
- 2021-02-10 07:39
有了就不贴了@
- 2楼网友:有钳、任性
- 2021-02-10 06:11
WebClient也行啊
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯