62,046
社区成员
发帖
与我相关
我的任务
分享
public static bool downloadFile(string url, string savePath, ref Exception ex,ref int fileSize)
{
HttpWebRequest req=null;
HttpWebResponse res=null;
System.IO.Stream stream = null;
bool result = false;
System.GC.Collect();
try
{
req = HttpWebRequest.Create(url) as System.Net.HttpWebRequest;
req.CookieContainer = new CookieContainer();
req.AllowAutoRedirect = true;
req.KeepAlive = false;
req.Timeout = 60 * 1000;
req.ServicePoint.ConnectionLeaseTimeout = 2 * 60 * 1000;
// req.ServicePoint.ConnectionLimit = int.MaxValue;
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1";
req.Headers.Add("Accept-Language", "en-us,en;q=0.5");
req.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
req.Headers.Add("Accept-Encoding", "gzip, deflate");
res = req.GetResponse() as System.Net.HttpWebResponse;
stream = res.GetResponseStream();
byte[] buffer = new byte[1 * 1024];
int bytesProcessed = 0;
System.IO.FileStream fs = new FileStream(savePath, FileMode.Create, FileAccess.Write);
int bytesRead;
do
{
bytesRead = stream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, bytesRead);
bytesProcessed += bytesRead;
}
while (bytesRead > 0);
fs.Flush();
fs.Close();
result = true;
fileSize = bytesProcessed;
}
catch (Exception exception)
{
ex = exception;
}
finally
{
if (stream != null)
{
stream.Close(); stream.Dispose();
}
if (req != null)
{
req.Abort();
req = null;
}
if (res != null)
{
res.Close();
res = null;
}
}
return result;
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Runtime.InteropServices.ComTypes;
namespace WebMonitor
{
/// <summary>
/// ActiveX 组件网页快照类
/// </summary>
public class WebSnapshot
{
/// <summary>
/// 实现网页快照
/// </summary>
/// <param name="pUnknown">Active组件</param>
/// <param name="bmpRect">Rectangle对象</param>
/// <returns></returns>
public Bitmap TakeSnapshot(object pUnknown, Rectangle bmpRect)
{
if (pUnknown == null)
return null;
//必须为com对象
if (!Marshal.IsComObject(pUnknown))
return null;
//IViewObject 接口
UnsafeNativeMethods.IViewObject ViewObject = null;
IntPtr pViewObject = IntPtr.Zero;
//新建一个bmp图片
Bitmap pPicture = new Bitmap(bmpRect.Width, bmpRect.Height);
//新建一个画板
Graphics hDrawDC = Graphics.FromImage(pPicture);
//获取接口
object hret = Marshal.QueryInterface(Marshal.GetIUnknownForObject(pUnknown),
ref UnsafeNativeMethods.IID_IViewObject, out pViewObject);
try
{
ViewObject = Marshal.GetTypedObjectForIUnknown(pViewObject, typeof(UnsafeNativeMethods.IViewObject)) as UnsafeNativeMethods.IViewObject;
//调用Draw方法
ViewObject.Draw((int)DVASPECT.DVASPECT_CONTENT,
-1,
IntPtr.Zero,
null,
IntPtr.Zero,
hDrawDC.GetHdc(),
new NativeMethods.COMRECT(bmpRect),
null,
IntPtr.Zero,
0);
Marshal.Release(pViewObject);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw ex;
}
hDrawDC.Dispose();
return pPicture;
}
}
}
/// <summary>
/// 保存网页快照
/// </summary>
/// <param name="url"></param>
/// <param name="path"></param>
/// <param name="Width"></param>
/// <param name="Height"></param>
/// <returns></returns>
public void SaveSnapshot(string url, string path, int Width, int Height)
{
//初始化窗体
InitComobject(url);
int scrollWidth = this.browser.Document.Body.ScrollRectangle.Width;
int scrollHeight = this.browser.Document.Body.ScrollRectangle.Height;
this.browser.Width = scrollWidth;
this.browser.Height = scrollHeight;
if (Width == 0)
{
Width = scrollWidth;
}
if (Height == 0)
{
Height = scrollHeight;
}
//截取网页快照封装到位图
WebSnapshot snapshot = new WebSnapshot();
using (Bitmap bitmap = snapshot.TakeSnapshot(this.browser.ActiveXInstance, new Rectangle(0, 0, Width, Height)))
{
bitmap.Save(path, ImageFormat.Jpeg); //这有个GDI+ 中发生一般性错误异常,偶发性的,资源没有释放的问题,文件访问的问题,或者其他问题
}
browser.Dispose();
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="url"></param>
protected void InitComobject(string url)
{
//this.browser.ScriptErrorsSuppressed = false;
this.browser.ScrollBarsEnabled = false;
this.browser.Navigate(url);
//SuppressScriptErrorsOnly(this.browser);
//保证页面完全加载
while (this.browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
//挂起一切动态元素
this.browser.Stop();
if (this.browser.ActiveXInstance == null)
{
throw new Exception("实例不能为空");
}
}