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:登录时使用@ ...
随机推荐
- 【UML】UML序列图总结
序列图主要用于展示对象之间交互的顺序. 序列图将交互关系表示为一个二维图. 纵向是时间轴,时间沿竖线向下延伸. 横向轴代表了在协作中各独立对象的类元角色. 类元角色用生命线表示. 当对象存在时,角色用 ...
- paip.杀不死进程的原因--僵尸进程的解决.txt
paip.杀不死进程的原因--僵尸进程的解决.txt 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn ...
- Atitit.web三大编程模型 Web Page Web Forms 和 MVC
Atitit.web三大编程模型 Web Page Web Forms 和 MVC 1. 编程模型是 Web Forms 和 MVC (Model, View, Controller). 2. ...
- paip 自定义输入法多多输入法词库的备份导出以及导入
paip 自定义输入法词库的备份导出以及导入 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/ ...
- iOS开发--JSON
1.什么是JSON? JSON(JavaScript Object Notation)在网络传输中几乎无处不在,JSON是一种轻量级的数据交换格式,是基于JavaScript(Standard ECM ...
- Leetcode 8 String to Integer (atoi) 字符串处理
题意:将字符串转化成数字. 前置有空格,同时有正负号,数字有可能会溢出,这里用long long解决(leetcode用的是g++编译器),这题还是很有难度的. class Solution { pu ...
- 结构体快排回顾(sort)
一般来说,我做竞赛的时候排序一般用快排 很快很方便 普通sort(从小到大) sort(a,a+n); 直接贴一段代码吧,包含了vector,sort,结构体等简单东西综合 #include < ...
- JQ选择器逐一测试
在web开发中大部分时间都在查找DOM元素和对DOM元素进行控制. 从上面就知道JQ为什么那么流行,因为它极大的缩短对DOM元素的查找和控制,让开发更快. 而它的对查找DOM的方法也很方便,这归类为选 ...
- listview可见再加载图片
对于,listView如果同时含有大量文字和图片,那么对于用户,如果不需要滑动到后面,那么此时去加载网络图片,显然是耗费流量的. 此时可以做一些优化: listView.getRefreshableV ...
- Linux/Unix 怎样找出并删除某一时间点的文件(转)
在Linux/Unix系统中,我们的应用每天会产生日志文件,每天也会备份应用程序和数据库,日志文件和备份文件长时间积累会占用大量的存储空间,而有些日志和备份文件是不需要长时间保留的,一般保留7天内的文 ...