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:登录时使用@ ...
随机推荐
- 微软 PowerShell Script Explorer 满血复活,正式发布
一年前的今天,微软在其Windows PowerShell官方博客声明中止 ‘Script Explorer’ 应用程序的开发. 一年后的今天,微软为其Script Explorer注入了新的生命.一 ...
- jenkins 插件开发资料
jenkins plugin 开发:documenthttp://hudson-ci.org/docs/index.htmlhttps://wiki.jenkins-ci.org/display/JE ...
- 使用Swift代码演示Cocoa框架
通过使用简单的代码学习Cocoa框架,每一个例子都通过代码和StoryBoard实现,并且总结他们的各自特点 所有完整代码将会托管到github库,https://github.com/land-pa ...
- chrome浏览器扩展的事件处理
关于chrome扩展开发的栗子已经有很多了,问问度娘基本能满足你的欲望, 我想说的是扩展和页面间的数据传递问题. 我们知道写扩展有个必须的文件就是“manifest.json”, 这个里面定义了一个和 ...
- 使用vs自带的性能诊断工具
visual studio是个强大的集成开发环境,内置了程序性能诊断工具.下面通过两段代码进行介绍. static void Main( string[] args) { Test1(); Test2 ...
- asynchttpClient框架关于多文件批量上传的问题,改用xUtil
RequestParams params = new RequestParams(); params.add("ordernum",ordernum); params.add(&q ...
- Android图片处理-图片压缩处理
这里先重复温习一下上一篇,调用相册获取图片: /*** * 这个是调用android内置的intent,来过滤图片文件 ,同时也可以过滤其他的 */ Intent intent = new Inten ...
- adb uninstall
adb shell pm list packages adb uninstall com.pa.pfac
- 如何向Openstack社区提交一个新项目
前几天有个朋友问我:自己有一个idea不错的项目,也把基本的框架写好了,想贡献到Openstack社区,却不知道应该怎么做.正好之前我有过类似的经历,那么来分享一下我是如何向Openstack社区提交 ...
- LDO/DC-DC区别总结(转)
电源是一个电子系统中不可缺少的非常重要的一部分.但是外接的电源通常不能够完全提供系统中需要的所有的电源种类.因此带来了电源电压的变换问题.常用的电源电压的变换芯片包括LDO和DC-DC两种.下面对这两 ...