/*************************************************************************************
* 文 件 名: 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. 挑灯熬夜看《Build 2015 Keynote》图文笔记

    又是一年微软Build大会时间,网络上流传各种微软新品发布的消息终于也要揭晓了,一直熬夜到凌晨3点,好久没有这么兴奋了. 微软给力的很嘛! Satya nadella开始讲解 首先回顾微软的传统和技术 ...

  2. Wijmo 2016 V2 强势发布!

    Angular 2 支持 Wijmo 对 Angular 2 提供了全面的支持.我们一直在紧跟Angular 2 开发团队的步伐,对其发布的最新候选版本提供支持. 了解更多关于Angular 2 的支 ...

  3. windows本地自动集成代码+SSH服务器配置

    在windows环境下使用Jenkins自动集成代码 描述: 局域网电脑A是本地服务器,安全win7系统,安装了Jenkins:现在想让A成为测试服务器,需要隔一段时间从SVN里同步出最新的代码,供客 ...

  4. Nagios学习笔记四:基于NRPE监控远程Linux主机

    1.NRPE简介 Nagios监控远程主机的方法有多种,其方式包括SNMP.NRPE.SSH和NCSA等.这里介绍其通过NRPE监控远程Linux主机的方式. NRPE(Nagios Remote P ...

  5. atitit...触发器机制 ltrigger mechanism sumup .的总结O8f

    atitit...触发器机制  ltrigger  mechanism sumup .的总结O8f 1. 触发器的类型 1 2. 实现原理 1 3. After触发器 Vs Instead Of触发器 ...

  6. paip.hql的调试故障排查流程总结

    paip.hql的调试故障排查流程总结 环境.myeclipse7.0 1 Hql的调试工具myeclipxe默认工具.../Hibernate8IDE 1 故障的排除方法overview 1 Hql ...

  7. 理解Certificate、App Id、Identifiers 和 Provisioning Profile

    做真机测试的时候,按照网上的流程,走通了,当时没有注意各种证书等的意思.现在做消息推送,需要各种证书.APP ID信息,为了更好的理解这个过程,所以整理了网上关于证书等的相关资料.方便自己和有需要的朋 ...

  8. JAVA学习Swing章节JPanel和JScrollPane面板的简单学习

    package com.swing; import java.awt.Container; import java.awt.GridLayout; import javax.swing.JButton ...

  9. 两两组合覆盖测试用例设计工具:PICT

    两两组合覆盖测试用例设计工具:PICT 2016-08-31 目录 1 成对测试简介2 PICT使用  2.1 安装 PICT  2.2 使用PICT3 PICT算法  3.1 准备阶段  3.2 产 ...

  10. struts2 iterator排序

    因为存入数据库的数据可能不是按照我们想要的方式存进去的.这就导致取出来的时候,不是按照我们想要的方式排序.这时候就要利用struts2的iterator排序功能,按照我们想要的顺序排列. 首先.一个付 ...