pdf下载整理:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net; namespace PdfDownload
{
/// <summary>
/// PDF 下载工具
/// </summary>
public class PDF : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var url = context.Request["url"];
var title = context.Request["title"];
var response = context.Response;
if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(title))
{
response.Charset = System.Text.Encoding.UTF8.ToString();
response.ContentEncoding = System.Text.Encoding.UTF8;
try
{
HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(url);
request.Timeout = 5000;
request.ReadWriteTimeout = 1000;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
using (var remoteResponse = request.GetResponse())
{
var length = remoteResponse.ContentLength;
if (length != -1)
{
response.ContentType = "application/pdf";
response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(title));
response.AppendHeader("Content-Length", length.ToString());
byte[] buffer = new byte[512 * 1024];
using (var stream = remoteResponse.GetResponseStream())
{
var position = 0;
while ((position = stream.Read(buffer, 0, buffer.Length)) > 0)
{
response.OutputStream.Write(buffer, 0, position);
}
}
response.Flush();
return;
}
}
}
catch (Exception ex)
{
response.ContentType = "html/text";
System.Text.StringBuilder builder = new System.Text.StringBuilder();
builder.Append("URL:");
builder.AppendLine(url);
builder.AppendLine(ex.ToString());
response.Write(builder.ToString());
}
}
else
{
context.Response.StatusCode = 404;
}
} public string GetWindSessionID(HttpContext context)
{
string str = context.Request.Headers["wind.sessionid"];
if (string.IsNullOrEmpty(str))
{
str = context.Request.QueryString["wind.sessionid"];
if (string.IsNullOrEmpty(str) && (context.Request.Cookies["wind.sessionid"] != null))
{
str = context.Request.Cookies["wind.sessionid"].Value;
}
}
return str;
} public bool IsReusable
{
get
{
return true;
}
}
}
}

  

最新文章

  1. Vim的分割窗口split命令
  2. 清理Oracle安装目录里的一些日志信息
  3. CodeIgniter 定义“全局变量-global variable”,可以在所有controller,model和view中使用
  4. hibernate一对多映射实现
  5. javascript模板方法模式
  6. ios开发经典语录锦集
  7. C语言中 移位操作运算
  8. .net 中的相等性比较
  9. 检验金额合法性, 只能是正数 或小数(常用js总结)
  10. Saving custom fields in production order
  11. R语言命令汇总
  12. 深入理解JVM(8)——类加载的时机
  13. 关于Gerrit code review 介绍与安装
  14. Linux下C语言执行shell命令
  15. 移动端js触摸事件大全
  16. P2279 消防局的设立(贪心+dp)
  17. HTML 脚本 (Script) 实例
  18. Java设计模式(6)桥模式(Bridge模式)
  19. Struts中的常量
  20. jQuery正则:电话、身份证、邮箱简单校验

热门文章

  1. 使用JS验证文件类型
  2. openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 三
  3. 剑指 Offer——和为 S 的两个数字
  4. leetcode个人题解——#39 Combination Sum
  5. ecshop以及一些需要注意的
  6. 补充的css知识点
  7. Scrum立会报告+燃尽图(Beta阶段第一次)
  8. “Hello World!”团队第七次Scrum立会
  9. VMware提示无法打开内核设备 \\.\Global\vmx86: 系统找不到指定的文件解决方案
  10. python 动态获取当前运行的类名和函数名的方法