采用httphandler去中转访问认证服务器。以下代码实现得是从认证远程服务器上下载资源到本地客户端。但实际连接到远程服务器上,下载文件所花费得时间非常长。大家分析下代码?看看程序所执行得时间花费在哪里,并减少这个时间花费。我认为该程序所花费太多时间,是发生在stream流写到内存流上。谢谢。讨论讨论。
页面上调用方法:
<a href="/aklib.aspx?fn=文件完整路径" target="_blank">文件名</a>
Code
public class AutoDownload : IHttpHandler
{
#region IHttpHandler Members
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
try
{
string fileName = SoFContext.Current.GetStringInQs("fn", string.Empty);
string siteUrl= System.Configuration.ConfigurationManager.AppSettings["SiteUrl"].ToString();
string user = System.Configuration.ConfigurationManager.AppSettings["FtpUser"].ToString();
string passWord = System.Configuration.ConfigurationManager.AppSettings["FtpPassWord"].ToString();
string filePath = string.Empty;
filePath = siteUrl+ fileName;
string file = Path.GetFileName(filePath);
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file, Encoding.UTF8));
HttpWebRequest httpWebRequest;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(filePath);
NetworkCredential credential;
if (!string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(passWord))
{
credential = new NetworkCredential(user, passWord);
httpWebRequest.Credentials = credential;
}
using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
using (Stream responseStream = httpWebResponse.GetResponseStream())
{
byte[] arrayByte;
byte[] buffer = new byte[4096];
using (MemoryStream stmMemory = new MemoryStream())
{
int count = 0;
do
{
count = responseStream.Read(buffer, 0, buffer.Length);
stmMemory.Write(buffer, 0, count);
} while (count != 0);
arrayByte = stmMemory.ToArray();
HttpContext.Current.Response.AppendHeader("Content-Length", arrayByte.Length.ToString());
HttpContext.Current.Response.BinaryWrite(arrayByte);
HttpContext.Current.Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.Fail(ex.Message);
System.Diagnostics.Debug.Fail(ex.StackTrace);
}
}