惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
Intezer
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
N
News | PayPal Newsroom
T
Tenable Blog
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
P
Privacy International News Feed
IT之家
IT之家
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
博客园_首页
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
NISL@THU
NISL@THU
I
InfoQ
D
DataBreaches.Net
有赞技术团队
有赞技术团队
K
Kaspersky official blog
Security Latest
Security Latest
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
AI
AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic

博客园 - Samgle

解决删除域用户Exception from HRESULT: 0x80072030 PDF-XChange 3.0 / Pro4.0注册码 Welcome to the Live Labs Pivot Technical Preview 修复 VS2008 asp.net 设计视图 失效/工具-选项[Html设计视图]出现"加载此属性页时出错" 方案 对于JavaScript的 Stack overflow at line 错误总结 - Samgle 10 Free Chart Scripts 如何让mvc运行在IIS6.0上? (ASP.NET MVC on IIS 6 Walkthrough) Live.com MSN 在线状态查询接口(JSON数据输出) - Samgle BIND9.5b1 for windows 2003 Google Maps by asp.net API集成-S.Sams Olympic Medal 如何在asp.net中用jQuery实现便捷的提示功能和表单验证功能-S.Sams jQuery jGrow 改进版 通过代理访问 http://sourceforge.net/ 常用简易JavaScript函数 世界真細小 (原曲: It's A Small World), 作曲: Richard She 做下笔记, Response 文件流类型 推荐微软官方Windows Server 2008实战攻略系列下载 Most used CSS tricks
HttpRequest重写,解决资源占用/链接超时/分块下载事件通知 问题。
Samgle · 2015-06-18 · via 博客园 - Samgle
/*************************************************************************************
* 文 件 名: WebRequest.cs
* 创建时间: 2015-06-18
* 作    者: Sam Shum (s.sams @ msn.com)
* 说    明: 解决WebHttpRequest下载网页数据出现问题 
* 修改时间: 2015-06-19
* 修 改 人: Sam Shum
*************************************************************************************/

using

System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.IO.Compression; using System.Linq; using System.Net; using System.Text; using System.Web; namespace S.Sams.Common { public class HttpRequest { public HttpRequest() { //string Url = "http://localhost:5192/api/Cab/5"; //string httpMethod = "PUT"; //string httpContent = "{'Name': 'B90', 'Color': 'Green', 'Height': 1590, 'Width': 4500 }"; //Encoding httpCode = Encoding.Default; //Console.WriteLine(Get(Url, httpMethod, httpContent, httpCode)); setEncoding = Encoding.Default; } public HttpRequest(string url, string method) : base() { setUrl = url; setMethod = method; } /// <summary> /// 服务请求地址 http://www.aaa.com/ /// </summary> public string setUrl { get; set; } /// <summary> /// 服务请求方法:GET/POST/PUT/DELETE /// </summary> public string setMethod { get; set; } /// <summary> /// 设置服务请求数据类型 /// </summary> [DefaultValue("text/html")] public string setContentType { get; set; } /// <summary> /// 设置服务页面编码 /// </summary> public Encoding setEncoding { get; set; } public delegate void DownloadStartDelegate(int httpStatusCode); public delegate void DownloadProcessDelegate(long totalLength, long DownloadedByte, float percent); public delegate void DownloadEndDelegate(long totalLength); public event DownloadStartDelegate DownloadStart; public event DownloadProcessDelegate DownloadProcess; public event DownloadEndDelegate DownloadEnd; public string Get() { return Get(setUrl); } public string Get(string setUrl) { return Get(setUrl, "GET", null, setEncoding); } public string Get(string setUrl, string httpContent) { return Get(setUrl, "POSE", httpContent, setEncoding); } public string Get(string Url, string httpMethod, string httpContent, Encoding httpCode) { if (string.IsNullOrWhiteSpace(Url) || string.IsNullOrWhiteSpace(httpMethod)) { throw new ArgumentException("Url or HttpMethod 参数不能为空!"); } GC.Collect(); StringBuilder content = new StringBuilder(); HttpWebRequest request = null; HttpWebResponse response = null; StreamWriter sw = null; try { ServicePointManager.DefaultConnectionLimit = 512; request = WebRequest.Create(Url) as HttpWebRequest; request.Method = httpMethod; request.AllowAutoRedirect = true; request.KeepAlive = false; request.Accept = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; Maxthon 2.0)"; request.ContentType = setContentType; //"application/octet-stream"; request.Proxy = null; if (httpContent != null && !string.IsNullOrWhiteSpace(httpContent)) { using (sw = new StreamWriter(request.GetRequestStream())) { sw.Write(httpContent); } } using (response = request.GetResponse() as HttpWebResponse) { if (this.DownloadStart != null) this.DownloadStart((int)response.StatusCode); Stream stream = response.GetResponseStream(); if (response.ContentEncoding.ToLower().Contains("gzip")) stream = new GZipStream(stream, CompressionMode.Decompress); float percent = 0; long totalDownloadedByte = 0; long totalBytes = response.ContentLength; while (stream.CanRead) { byte[] buffer = new byte[1024]; int canrds = stream.Read(buffer, 0, 1024); totalDownloadedByte = canrds + totalDownloadedByte; percent = (float)totalDownloadedByte / (float)totalBytes * 100; if (DownloadProcess != null && canres > 0) DownloadProcess(totalBytes, totalDownloadedByte, percent); if (canrds == 0) break; content.Append(httpCode.GetString(buffer)); } if (DownloadEnd != null) DownloadEnd(totalBytes); stream.Dispose(); } } finally { if(request != null) request.Abort(); if(response != null) response.Dispose(); if(sw != null) sw.Dispose(); } GC.SuppressFinalize(this); return content.ToString(); /* Response.Write(Guid.NewGuid().ToString() + "<hr />"); Response.Write(Guid.NewGuid().ToString("N") + "<hr />"); Response.Write(GetHttpContent); Response.End(); */ } } }