C# HttpHelper
public enum HttpVerb
{
Get,
Post
} public class HttpHelper
{
private string _contentType = "application/x-www-form-urlencoded";
//private string _contentType = "application/json";
//private string _contentType = "text/xml";
private int _timeout = ; public string EndPoint { get; set; }
public HttpVerb Method { get; set; }
public string ContentType { get { return _contentType; } set { _contentType = value; } }
public string PostData { get; set; } /// <summary>
/// 默认15000毫秒
/// </summary>
public int Timeout { get { return _timeout; } set { _timeout = value; } } public HttpHelper(string endpoint)
{
EndPoint = endpoint;
Method = HttpVerb.Get;
ContentType = _contentType;
PostData = "{}";
} public string Post(string data)
{
var service = new Uri(EndPoint);
var request = WebRequest.CreateHttp(service);
request.Method = "POST";
request.Timeout = Timeout;
request.KeepAlive = true;
request.ContentLength = ;
request.ContentType = ContentType;
request.Accept = "application/json"; var bytes = Encoding.UTF8.GetBytes(data);
request.ContentLength = bytes.Length;
using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, , bytes.Length);
} using (var response = (HttpWebResponse)request.GetResponse())
{
var responseValue = string.Empty;
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format(CultureInfo.CurrentCulture, "Request failed. Received HTTP {0}", response.StatusCode);
throw new WebException(message);
}
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
return responseValue;
}
} /// <summary>
/// Request server
/// </summary>
/// <param name="parameters">eg. "?q=1234"</param>
/// <returns></returns>
public string Get(string parameters)
{
var service = new Uri(EndPoint + parameters);
var request = WebRequest.CreateHttp(service);
request.Method = "GET";
request.Timeout = Timeout;
request.KeepAlive = true;
request.ContentLength = ;
request.ContentType = ContentType; using (var response = (HttpWebResponse)request.GetResponse())
{
var responseValue = string.Empty;
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format(CultureInfo.CurrentCulture, "Request failed. Received HTTP {0}", response.StatusCode);
throw new WebException(message);
}
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
return responseValue;
}
} /// <summary>
/// 下载更新二进制文件
/// </summary>
/// <param name="parameters">eg. "?q=1234"</param>
/// <returns></returns>
public byte[] DownLoad(string parameters)
{
var service = new Uri(EndPoint + parameters);
var request = WebRequest.CreateHttp(service);
request.Timeout = Timeout;
request.KeepAlive = true;
request.Method = Method.ToString().ToUpper(CultureInfo.CurrentCulture);
request.ContentLength = ;
request.ContentType = ContentType; if (Method == HttpVerb.Post)
{
var bytes = Encoding.UTF8.GetBytes(PostData);
request.ContentLength = bytes.Length; using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, , bytes.Length);
}
} using (var response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format(CultureInfo.CurrentCulture, "Request failed. Received HTTP {0}", response.StatusCode);
throw new WebException(message);
}
using (var result = new MemoryStream())
{
const int bufferLen = ;
var buffer = new byte[bufferLen];
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
var len = responseStream.Read(buffer, , bufferLen);
while (len > )
{
result.Write(buffer, , len);
len = responseStream.Read(buffer, , bufferLen);
}
}
}
return result.ToArray();
}
}
} }
C# HttpHelper的更多相关文章
- HttpHelper
/// <summary> /// 类说明:HttpHelper类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理 /// 重要提 ...
- 抓取百万知乎用户信息之HttpHelper的迭代之路
什么是Httphelper? httpelpers是一个封装好拿来获取网络上资源的工具类.因为是用http协议,故取名httphelper. httphelper出现的背景 使用WebClient可以 ...
- Lind.DDD.Utils.HttpHelper里静态对象引出的Http超时问题
回到目录 Lind.DDD.Utils.HttpHelper组件主要实现了对HTTP的各种操作,如Get,Post,Put和Delete,它属于最纯粹的操作,大叔把它封装的目的主要为了实现与API安全 ...
- Sokcet方式请求HTTP/HTTPS的封装类HttpHelper
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; ...
- HttpHelper类
using System;using System.Collections.Generic;using System.Text;using System.Net;using System.IO;usi ...
- HttpHelper类登录淘宝联盟并下载淘宝客订单xls
本次开发环境与工具如下:IE9.0浏览器 + IE抓包插件HttpWatch +WIN7 64位系统 + VS2005 IDE + .NET 2.0框架本想上传HttpWatch抓包插件,但由于文件超 ...
- C# HttpHelper 采集
httphelper http://www.sufeinet.com/thread-6-1-1.html
- [C#HttpHelper]类1.4正式版教程与升级报告
[C#HttpHelper]类1.4正式版教程与升级报告 导读 1.升级报告 2.HttpHelper1.4正式版下载 3.HttpHelper类使用方法, 4.最简单的Post与Get的写法 ...
- 使用苏飞httphelper开发自动更新发布文章程序
最近新上线了一个网站,专门收集网上签到赚钱,有奖活动等等的网站 我就要集分宝 http://www.591jfb.com.新建立 了一个栏目"每日更新",这样就需要每天都登录到网站 ...
- 完整HttpHelper类
[代码] [C#]代码 using System; using System.Collections.Generic; using System.Text; using System.Net; usi ...
随机推荐
- WPF 平板上按钮点击不触发,鼠标点击触发的两种解决方法
今天运行在windows平板上的程序,有个功能是弹出子窗体,点弹出窗体的关闭按钮,要点好几次才能触发.网上找了找,也有人与我类似的情形. 解决方法如下: public static void Disa ...
- 原生 javaScript 百叶窗 效果的实现及原理介绍
百叶窗大家都见过吧!如图: 原理: 如图所示,空心格子就好比是每个li,给它设定相对定位属性,设置overflow:hidden: 黑块为li子元素,高度为li的2倍,设置absolute属性,我们正 ...
- 知识记录——CSS规范(文章内容为转载)
原作者信息 作者:词晖 链接:http://www.zhihu.com/question/19586885/answer/48933504 来源:知乎 著作权归原作者所有,转载请联系原作者获得授权. ...
- iOS 应用隐藏状态栏
有时候在 Storyboard 和 target 里的设定并不足以使得应用能够完全隐藏状态栏. 这时候需要到 Info.plist,添加一项:View controller-based status ...
- java—实现一个监听器HttpServletRequest的创建销毁、在线人数 (56)
在JavaWeb中的监听器分类 在Javaweb中存在三个被监听对象: HttpServletRequest HttpSessoin ServletContext 监听者 被监听者 监听到事件对象 H ...
- Requests Header | Http Header
Requests Header | Http Header Header 解释 示例 Accept 指定客户端能够接收的内容类型 Accept: text/plain, text/html Accep ...
- DESTOON从CSRF到GETSHELL
本文作者:薄荷糖微微凉 Destoon B2B网站管理系统是一套完善的B2B(电子商务)行业门户解决方案.系统基于PHP+MySQL开发,采用B/S架构,模板与程序分离,源码开放.模型化的开发思路,可 ...
- [javascript]—jQuery解析本地 XML 文档
Create a jQuery object using an XML string and obtain the value of the title node. <!doctype html ...
- VMware 虚拟机报错解决
Could not apply the stored configuration for monitors none of the selected modes were compatible wit ...
- jee-oxygen版eclipse的安装与卸载 maven配置
Eclipse 是一个开放源代码的.基于Java的可扩展开发平台,是大部分JAVA编程学习者入门的编程工具.Eclipse 是开发java的一个工具,Eclipse需要JDK中的JRE支持才能启动,所 ...