using CsharpHttpHelper.Enum; using System; using System.Net; using System.Security.Cryptography.X509Certificates; using System.Text; namespace CsharpHttpHelper { /// /// Http请求参考类 Copyright:http://www.httphelper.com/ /// public class HttpItem { private string _Method = "GET"; private int _Timeout = 100000; private int _ReadWriteTimeout = 30000; private bool _KeepAlive = true; private string _Accept = "text/html, application/xhtml+xml, */*"; private string _ContentType = "text/html"; private string _UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"; private bool _expect100continue = false; private DateTime? _IfModifiedSince = null; private bool _isGzip = false; private DecompressionMethods _AutomaticDecompression = DecompressionMethods.None; private PostDataType _PostDataType = PostDataType.String; private bool _AutoRedirectCookie = false; private ResultCookieType _ResultCookieType = ResultCookieType.String; private bool _isUpdateCookie = false; private CookieContainer _CookieContainer = new CookieContainer(); private ICredentials _ICredentials = CredentialCache.DefaultCredentials; private bool isToLower = false; private bool allowautoredirect = false; private int connectionlimit = 1024; private ResultType resulttype = ResultType.String; private WebHeaderCollection header = new WebHeaderCollection(); private IPEndPoint _IPEndPoint = null; private bool _isReset = false; /// /// 请求URL必须填写 /// public string URL { get; set; } /// /// 请求方式默认为GET方式,当为POST方式时必须设置Postdata的值 /// public string Method { get { return _Method; } set { _Method = value; } } /// /// 默认请求超时时间 /// public int Timeout { get { return _Timeout; } set { _Timeout = value; } } /// /// 默认写入和读取Post数据超时间 /// public int ReadWriteTimeout { get { return _ReadWriteTimeout; } set { _ReadWriteTimeout = value; } } /// /// 设置Host的标头信息 /// public string Host { get; set; } /// /// 获取或设置一个值,该值指示是否与 Internet 资源建立持久性连接默认为true。 /// public bool KeepAlive { get { return _KeepAlive; } set { _KeepAlive = value; } } /// /// 请求标头值 默认为text/html, application/xhtml+xml, */* /// public string Accept { get { return _Accept; } set { _Accept = value; } } /// /// 请求返回类型默认 text/html /// public string ContentType { get { return _ContentType; } set { _ContentType = value; } } /// /// 客户端访问信息默认Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) /// public string UserAgent { get { return _UserAgent; } set { _UserAgent = value; } } /// /// 来源地址,上次访问地址 /// public string Referer { get; set; } /// /// 获取或设置用于请求的 HTTP 版本。返回结果:用于请求的 HTTP 版本。默认为 System.Net.HttpVersion.Version11。 /// public Version ProtocolVersion { get; set; } /// /// 获取或设置一个 System.Boolean 值,该值确定是否使用 100-Continue 行为。如果 POST 请求需要 100-Continue 响应,则为 true;否则为 false。默认值为 true。 /// public bool Expect100Continue { get { return _expect100continue; } set { _expect100continue = value; } } /// /// 设置请求将跟随的重定向的最大数目 /// public int MaximumAutomaticRedirections { get; set; } /// /// 获取和设置IfModifiedSince,默认为当前日期和时间 /// public DateTime? IfModifiedSince { get { return _IfModifiedSince; } set { _IfModifiedSince = value; } } /// /// 是否执行Gzip解压 默认为否 /// public bool IsGzip { get { return _isGzip; } set { _isGzip = value; } } /// /// 返回数据编码默认为NUll,可以自动识别,一般为utf-8,gbk,gb2312 /// public Encoding Encoding { get; set; } /// /// 设置或获取Post参数编码,默认的为Default编码 /// public Encoding PostEncoding { get; set; } /// /// /// public DecompressionMethods AutomaticDecompression { get { return _AutomaticDecompression; } set { _AutomaticDecompression = value; } } /// /// Post的数据类型 /// public PostDataType PostDataType { get { return _PostDataType; } set { _PostDataType = value; } } /// /// Post请求时要发送的字符串Post数据 /// public string Postdata { get; set; } /// /// Post请求时要发送的Byte类型的Post数据 /// public byte[] PostdataByte { get; set; } /// /// Cookie对象集合 /// public CookieCollection CookieCollection { get; set; } /// /// 请求时的Cookie /// public string Cookie { get; set; } /// /// 请求时当设置allowautoredirect=true时是否自动处理Cookie /// public bool AutoRedirectCookie { get { return _AutoRedirectCookie; } set { _AutoRedirectCookie = value; } } /// /// Cookie返回类型,默认的是只返回字符串类型 /// public ResultCookieType ResultCookieType { get { return _ResultCookieType; } set { _ResultCookieType = value; } } /// /// 是否自动将Cookie自动更新为请求所获取的新Cookie值 默认为False /// public bool IsUpdateCookie { get { return _isUpdateCookie; } set { _isUpdateCookie = value; } } /// /// Cookie对象的集合容器 模式Cookie,可容纳N个CookieCollection对象 /// public CookieContainer CookieContainer { get { return _CookieContainer; } set { _CookieContainer = value; } } /// /// 证书绝对路径 /// public string CerPath { get; set; } /// /// 证书密码 /// public string CerPwd { get; set; } /// /// 设置509证书集合 /// public X509CertificateCollection ClentCertificates { get; set; } /// /// 获取或设置请求的身份验证信息。 /// public ICredentials ICredentials { get { return _ICredentials; } set { _ICredentials = value; } } /// /// 是否设置为全文小写,默认为不转化 /// public bool IsToLower { get { return isToLower; } set { isToLower = value; } } /// /// 支持跳转页面,查询结果将是跳转后的页面,默认是不跳转 /// public bool Allowautoredirect { get { return allowautoredirect; } set { allowautoredirect = value; } } /// /// 最大连接数 /// public int Connectionlimit { get { return connectionlimit; } set { connectionlimit = value; } } /// /// 设置代理对象,不想使用IE默认配置就设置为Null,而且不要设置ProxyIp /// public WebProxy WebProxy { get; set; } /// /// 代理Proxy 服务器用户名 /// public string ProxyUserName { get; set; } /// /// 代理 服务器密码 /// public string ProxyPwd { get; set; } /// /// 代理 服务IP,如果要使用IE代理就设置为ieproxy /// public string ProxyIp { get; set; } /// /// 设置返回类型String和Byte /// public ResultType ResultType { get { return resulttype; } set { resulttype = value; } } /// /// header对象 /// public WebHeaderCollection Header { get { return header; } set { header = value; } } /// /// 设置本地的出口ip和端口 /// ] /// /// item.IPEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.1"),80); /// public IPEndPoint IPEndPoint { get { return _IPEndPoint; } set { _IPEndPoint = value; } } /// /// 当出现“请求被中止: 未能创建 SSL/TLS 安全通道”时需要配置此属性 /// public SecurityProtocolType SecurityProtocol { get; set; } /// /// 是否重置request,response的值,默认不重置,当设置为True时request,response将被设置为Null /// public bool IsReset { get { return _isReset; } set { _isReset = value; } } } }