pdf 下载整理

pdf下载整理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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;
            }
        }
    }
}

  

posted @   fo0ol  阅读(110)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示