/*************************************************************************************
* 文 件 名: 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重写,解决资源战胜/链接超时/分块下载事件通知 问题。的更多相关文章

  1. Python安装第三方包(模块/工具)出现链接超时,网速慢,安装不上的问题如何解决

    之前我的电脑重新装了系统以后,发现安装完Python后, 使用pip linstall 安装第三方包的时候,网速慢的一匹 有时候只有几百b/s ,而且还动不动就会出现无法安装,链接超时等问题. 今天我 ...

  2. paip.点击每个网页链接都提示下载的解决。

    paip.点击每个网页链接都提示下载的解决.   作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn. ...

  3. Springboot解决资源文件404,503等特殊报错,无法访问

    Springboot解决资源文件404,503等特殊报错 原文链接:https://www.cnblogs.com/blog5277/p/9324609.html 原文作者:博客园--曲高终和寡 ** ...

  4. Oracle数据库链接超级慢或者总提示链接超时

    Centos6  今天tomcat应用程序链接数据库总提示链接超时,客户端工具通过tnsnames连接数据库实例进行操作也超级慢, 实在无法忍受, 重启实例试试吧,重启了还是不好使,还是很慢很慢,无比 ...

  5. ecshop 完美解决动态ip登录超时和购物车清空问题

    ecshop 完美解决动态ip登录超时和购物车清空问题 ECSHOP模板/ecshop开发中心(www.68ecshop.com) / 2014-05-06 前一段时间,ECSHOP开发中心的一个客户 ...

  6. 解决远程桌面链接时出现"The RPC server is unavailable."或"RPC服务器不可用"的问题

    解决远程桌面链接时出现"The RPC server is unavailable."或"RPC服务器不可用"的问题 解决远程桌面链接时出现"The ...

  7. 解决SQL查询总是超时已过期

    解决SQL查询总是超时已过期 .在WIN8里提示:OLE DB 或 ODBC 错误 : 查询超时已过期; HYT00 1.由于数据库设计问题造成SQL数据库新增数据时超时 症状:   Microso ...

  8. 微信端解决a标签链接 失效的问题

    最近常碰到这个问题就是 在微信端点击a标签链接的时候,第一次正常界面跳转.但是,界面重新跳转回来再次点击a标签的话 .出现 界面不跳转,但是进度条加载完毕,点击多次页面无法跳转. 解决办法 在链接后边 ...

  9. oracle用plsql登陆出错,提示ORA-12170:TNS:链接超时 --------关闭防火墙试试

    oracle用plsql登陆出错,提示ORA-12170:TNS:链接超时 但是使用sqlplus可以连接 ping 本机127.0.0.1 显示一般故障 后关闭防火墙,问题解决. ps:登录时使用@ ...

随机推荐

  1. CSS行高——line-height

    初入前端的时候觉得CSS知道display.position.float就可以在布局上游刃有余了,随着以后工作问题层出不穷,才逐渐了解到CSS并不是几个style属性那么简单,最近看了一些关于行高的知 ...

  2. Win10年度更新开发必备:VS2015 Update 3正式版下载汇总

    微软在06月27日发布了Visual Studio 2015 Update 3 .在MSDN中微软也提供下载,而且MSDN的Visual Studio 2015 Update 3与官方免费下载的文件是 ...

  3. C语言实现单链表-04版

    前面的版本似乎没能让项目经理满意,他还希望这个链表有更多的功能: 我们接下来要解决几个比较简单的功能: Problem 1,更加友好的显示数据: 2,能够通过名字删除节点: Solution 首先我们 ...

  4. atitit.dw不能显示正确的百分比高度in dw的解决

    atitit.dw不能显示正确的百分比高度in dw的解决 div 设置35%的高度,三,不能正确的显示高度...环境dw cs6 但是设置161px奏能ok了...表明这个是dw的一个bug... ...

  5. Paip.Php Java 异步编程。推模型与拉模型。响应式(Reactive)”编程FutureData总结... 1

    Paip.Php  Java 异步编程.推模型与拉模型.响应式(Reactive)"编程FutureData总结... 1.1.1       异步调用的实现以及角色(:调用者 提货单) F ...

  6. paip.环境配置整合 ibatis mybatis proxool

    paip.环境配置整合 ibatis mybatis proxool  索引: ///////////1.调用 ///////////////2. ibatis 主设置文件  com/mijie/ho ...

  7. .NET Remoting学习笔记(一)概念

    目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 背景 自接触编程以来,一直听过这个名词Remotin ...

  8. 为ubuntu操作系统增加root用户

    1:当安装好虚拟机,安装好Ubuntu操作系统后,登陆的时候发现除了自己的设置的用户就是外来用户,其实Ubuntu中的root帐号默认是被禁用了的,所以登陆的时候没有这个账号,但是如果每次使用root ...

  9. SIFT 特征提取算法总结

    原文链接:http://www.cnblogs.com/cfantaisie/archive/2011/06/14/2080917.html   主要步骤 1).尺度空间的生成: 2).检测尺度空间极 ...

  10. iphone/ipad/ipod设置VPN(pptp连接方式)

    一.点击桌面上的-设置-图标进入设置(如图) 二.点击-通用-进入通用设置 三.点击-VPN-进入VPN设置(如图) 四.点击添加VPN设置进行设置 五.选择并连接