HttpRequest重写,解决资源战胜/链接超时/分块下载事件通知 问题。
/*************************************************************************************
* 文 件 名: 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 = ;
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 = ;
long totalDownloadedByte = ;
long totalBytes = response.ContentLength;
while (stream.CanRead)
{
byte[] buffer = new byte[];
int canrds = stream.Read(buffer, , );
totalDownloadedByte = canrds + totalDownloadedByte;
percent = (float)totalDownloadedByte / (float)totalBytes * ; if (DownloadProcess != null && canres > 0)
DownloadProcess(totalBytes, totalDownloadedByte, percent); if (canrds == )
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();
*/
} }
}
HttpRequest重写,解决资源战胜/链接超时/分块下载事件通知 问题。的更多相关文章
- Python安装第三方包(模块/工具)出现链接超时,网速慢,安装不上的问题如何解决
之前我的电脑重新装了系统以后,发现安装完Python后, 使用pip linstall 安装第三方包的时候,网速慢的一匹 有时候只有几百b/s ,而且还动不动就会出现无法安装,链接超时等问题. 今天我 ...
- paip.点击每个网页链接都提示下载的解决。
paip.点击每个网页链接都提示下载的解决. 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn. ...
- Springboot解决资源文件404,503等特殊报错,无法访问
Springboot解决资源文件404,503等特殊报错 原文链接:https://www.cnblogs.com/blog5277/p/9324609.html 原文作者:博客园--曲高终和寡 ** ...
- Oracle数据库链接超级慢或者总提示链接超时
Centos6 今天tomcat应用程序链接数据库总提示链接超时,客户端工具通过tnsnames连接数据库实例进行操作也超级慢, 实在无法忍受, 重启实例试试吧,重启了还是不好使,还是很慢很慢,无比 ...
- ecshop 完美解决动态ip登录超时和购物车清空问题
ecshop 完美解决动态ip登录超时和购物车清空问题 ECSHOP模板/ecshop开发中心(www.68ecshop.com) / 2014-05-06 前一段时间,ECSHOP开发中心的一个客户 ...
- 解决远程桌面链接时出现"The RPC server is unavailable."或"RPC服务器不可用"的问题
解决远程桌面链接时出现"The RPC server is unavailable."或"RPC服务器不可用"的问题 解决远程桌面链接时出现"The ...
- 解决SQL查询总是超时已过期
解决SQL查询总是超时已过期 .在WIN8里提示:OLE DB 或 ODBC 错误 : 查询超时已过期; HYT00 1.由于数据库设计问题造成SQL数据库新增数据时超时 症状: Microso ...
- 微信端解决a标签链接 失效的问题
最近常碰到这个问题就是 在微信端点击a标签链接的时候,第一次正常界面跳转.但是,界面重新跳转回来再次点击a标签的话 .出现 界面不跳转,但是进度条加载完毕,点击多次页面无法跳转. 解决办法 在链接后边 ...
- oracle用plsql登陆出错,提示ORA-12170:TNS:链接超时 --------关闭防火墙试试
oracle用plsql登陆出错,提示ORA-12170:TNS:链接超时 但是使用sqlplus可以连接 ping 本机127.0.0.1 显示一般故障 后关闭防火墙,问题解决. ps:登录时使用@ ...
随机推荐
- jenkins2 pipeline入门
本文通过简单的pipeline的实例和详细的讲解,能够学习基本pipeline的groovy用法,然后开始实现自己的pipeline job. 翻译和修改自:https://github.com/je ...
- 如何设置iframe高度自适应,在跨域的情况下能做到吗?
在页面上使用iframe来动态加载页面内容是网页开发中比较常见的方法.在父页面中给定一个不带滚动条的iframe,然后对属性src指定一个可加载的页面,这样当父页面被访问的时候,子页面可以被自动加载. ...
- iOS数据持久化-OC
沙盒详解 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文 ...
- Observer模式
拉模式. Observer实现了一种间接关系.可以向各种对象注册观察者.可以有效地管理依赖关系. 拉模式实现简单,且Subject和Observer可以成为类库中的可重用元素. 当被观察对象比较复杂, ...
- sqlserver自定义函数的创建与调用
sqlserver中有系统提供的函数,像avg.sum.getdate()等,用户还可以自定义函数. 用户自定义的函数包括:标量函数和表值函数,其中标量函数和系统函数的用法一样,表值函数根据主体的定义 ...
- 微软BI 之SSIS 系列 - 通过设置 CheckPoints 检查点来增强 SSIS Package 流程的重用性
开篇介绍 通常一个 ETL Package 是由多个控制流和数据流共同组成,有的时候 ETL 的步骤可能会比较多,整个流程执行下来的时间可能比较长.假设在 ETL Package 中包含5个Task, ...
- 如何管理和记录 SSIS 各个 Task 的开始执行时间和结束时间以及 Task 中添加|删除|修改的记录数
开篇语 在这篇日志中 如何在 ETL 项目中统一管理上百个 SSIS 包的日志和包配置框架 我介绍到了包级别的日志管理框架,那么这个主要是针对包这一个层级的 Log 信息,包括包开始执行和结束时间,以 ...
- [推荐]WebService开发知识介绍
[推荐]WebService开发知识介绍 WebService开发手册 http://wenku.baidu.com/view/df3992ce050876323112128a.html WebSe ...
- Inno setup 安装*.inf文件_示例
nno setup 调用*.Inf文件的条目区段名称_示例 首先自己编写一个INF文件来供 Inno setup 进行测试: ;复制以下代码到记事本然后另存为123.inf .然后把123.inf文件 ...
- VS2013中Python学习笔记[Django Web的第一个网页]
前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...