博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
带有用户名密码验证的远程文件下载
阅读量:6502 次
发布时间:2019-06-24

本文共 2709 字,大约阅读时间需要 9 分钟。

try            {                string getFileName = Url.Substring(Url.LastIndexOf("/") + 1);                HttpWebRequest WRequest;               // HttpWebResponse response = null;                Uri uri = new Uri(Url);                CredentialCache cc = new CredentialCache();                WRequest = (HttpWebRequest)HttpWebRequest.Create(uri);                WRequest.Credentials = new System.Net.NetworkCredential(DocLibConst.Con_SPUserName, DocLibConst.Con_PWD, DocLibConst.Con_Domain);                WRequest.PreAuthenticate = true;                WRequest.Method = "GET";                WRequest.AllowWriteStreamBuffering = false;                WRequest.SendChunked = false;                WRequest.KeepAlive = true;                WRequest.ContentLength = 0;                //WRequest.SendChunked = true;                //WRequest.ContentLength = 100000;                WRequest.Timeout = 30000;                WRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes("epapi:epapiadmin")));                WebResponse wr = WRequest.GetResponse();                Stream responseStream = wr.GetResponseStream();                MemoryStream mem = new MemoryStream();                //创建本地文件写入流                //Stream stream = new FileStream("D:\\xx.docx", FileMode.Create);                byte[] bArr = new byte[1024];                int size = responseStream.Read(bArr, 0, (int)bArr.Length);                while (size > 0)                {                    //stream.Write(bArr, 0, size);                    mem.Write(bArr, 0, size);                    size = responseStream.Read(bArr, 0, (int)bArr.Length);                }                //stream.Write(mem.ToArray(), 0, mem.ToArray().Length);                //stream.Close();                responseStream.Close();                Page.Response.ClearContent();                Page.Response.ClearHeaders();                Page.Response.AddHeader("Content-Disposition", "attachment; filename="                    + HttpUtility.UrlEncode(getFileName, System.Text.Encoding.UTF8));                Page.Response.AddHeader("Content-Length", mem.ToArray().Length.ToString());                Page.Response.AddHeader("Content-Transfer-Encoding", "binary");                Page.Response.ContentType = "application/octet-stream";                Page.Response.BinaryWrite(mem.ToArray());                Page.Response.Flush();                //page.Response.End();            }            catch (Exception ex)            {                throw ex;            }            finally            {                HttpContext.Current.Response.Close();            }

 

转载于:https://www.cnblogs.com/muxueyuan/p/5888616.html

你可能感兴趣的文章
php 魔术方法 说明
查看>>
Mysql
查看>>
POJ-1860-Currency Exchange
查看>>
跨越企业的“中等收入陷阱”
查看>>
Android 开发者必知的开发资源
查看>>
软件工程技术基础-(软件复用技术)
查看>>
给django视图类添加装饰器
查看>>
DVWA默认用户名密码
查看>>
简述 clearfix 的原理
查看>>
【Project Euler】530 GCD of Divisors 莫比乌斯反演
查看>>
luogu P1280 尼克的任务 序列DP
查看>>
获取文件最后修改时间的VC代码
查看>>
ThinkPHP子类继承Controller类的注意事项
查看>>
iphone UIView的一些基本方法理解
查看>>
sys.check_constraints
查看>>
vue问题
查看>>
Linux常用命令大全
查看>>
ThinkPHP 框架学习
查看>>
yii1框架,事务使用方法
查看>>
css3箭头效果
查看>>