最近把上面的方法整理了一下,给大家分享一下吧,不好的地方感谢大家留言指正,不多说了上代码吧!
/// <summary> /// 类说明:HttpHelps教程类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式 /// 编码日期:2011-09-13 /// 编 码 人: 苏飞 /// 联系方式:361983679 Email:sufei.1013@163.com Blogs:http://sufei.cnblogs.com /// </summary>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text; using System.Net; using System.IO; using System.Security.Cryptography.X509Certificates; using System.Net.Security; using System; /// <summary> ///HttpHelpers 主要是实现Http方式的Get和Post方式请求 /// </summary> public class HttpHelpers { /// <summary> /// 构造器实现默认属性的赋值 /// </summary> public HttpHelpers() { //设置请求方式为Post request.Method = "GET"; request.Accept = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/x-www-form-urlencoded"; } #region 所有的属性 //访问的页面地址 private string RequestURl { get; set; } //默认的编码 private Encoding encoding { get; set; } //HttpWebRequest对象用来发起请求 private HttpWebRequest request { get; set; } //获取影响流的数据对象 private HttpWebResponse response { get; set; } //证书文件X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\123.cer"); X509Certificate objx509 { get; set; } //请求方式目前只提供Post和Get方式 private string Method { get; set; } //Accept属性 private string Accept { get; set; } //ContentType属性 private string ContentType { get; set; } //UserAgent属性 private string UserAgent { get; set; } //Cookie列表 private CookieContainer cookie { get; set; } //需要返回的数据对象 private string reutrnDate { get; set; } //Post数据串 private string strPostdata { get; set; } #endregion #region 内部方法 //回调验证证书问题 private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { // 总是接受 return true; } /// <summary> /// 根据相传入的数据,得到相应页面数据 /// </summary> /// <param name="strPostdata">传入的数据Post方式,get方式传NUll或者空字符串都可以</param> /// <returns>string类型的响应数据</returns> private string GetHttpRequestData() { try { //是否要添加证书验证 if (objx509 != null) { //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); } //创建request对象 request = (HttpWebRequest)WebRequest.Create(RequestURl); //是否要添加证书验证 if (objx509 != null) request.ClientCertificates.Add(objx509); //是否带有Cookie值 if (cookie != null) request.CookieContainer = cookie; //当为post提交是需要填充数据 if (Method.Trim().ToLower() == "post") { byte[] buffer = encoding.GetBytes(strPostdata); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); } //得到请求的response using (response = (HttpWebResponse)request.GetResponse()) { //从这里开始我们要无视编码了 if (encoding == null) GetEonding(); //开始读取流并设置编码方式 using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { //从当前开始读取整个流数据,默认为0所以读取的是全部,并返回数据 reutrnDate = reader.ReadToEnd().ToString().Trim(); } } } catch (Exception) { //这里是在发生异常时返回的错误信息 reutrnDate = "String Error"; } return reutrnDate; } /// <summary> /// 得到response对象的编码类型 /// </summary> private void GetEonding() { if (response.CharacterSet.Trim() != "") encoding = System.Text.Encoding.GetEncoding(response.CharacterSet.Trim()); else encoding = System.Text.Encoding.UTF8; } #endregion #region 公开方法 /// <summary> /// 只设置一些简单参数的方式 /// </summary> /// <param name="_url">URl地址</param> /// <param name="_strPostdata">Post请求方式时传入的数据</param> /// <param name="_Method">请求方式GET或者POST可以为空默认为GET</param> /// <param name="_encoding">编码方式可以为空默认为UTF-8</param> /// <param name="_Accept">Accept属性</param> /// <param name="_ContentType">ContentType属性</param /// <param name="_UserAgent">UserAgent属性</param /// <param name="_cookie">CookieContainer列表</param /// <param name="_objx509">X509Certificate证书对象</param /// <returns>请求所得到的数据</returns> public string GetString_null(string _url, string _strPostdata, string _Method, Encoding _encoding, string _Accept, string _ContentType, string _UserAgent, CookieContainer _cookie, X509Certificate _objx509) { RequestURl = _url; Method = _Method; encoding = _encoding; Accept = _Accept; ContentType = _ContentType; UserAgent = _UserAgent; cookie = _cookie; objx509 = _objx509; return GetHttpRequestData(); } /// <summary> /// 只设置一些简单参数的方式 /// </summary> /// <param name="_url">URl地址</param> /// <param name="_strPostdata">Post请求方式时传入的数据</param> /// <param name="_Method">请求方式GET或者POST可以为空默认为GET</param> /// <param name="_encoding">编码方式可以为空默认为UTF-8</param> /// <returns>请求所得到的数据</returns> public string GetString_type(string _url, string _Method, Encoding _encoding) { RequestURl = _url; Method = _Method; encoding = _encoding; return GetHttpRequestData(); } //下面大家自己可以多写几种常用的,呵呵 #endregion }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索string
, 数据
, 对象
, 编码
, 属性
using
post cookie、httppost cookie、python post cookie、curl post cookie、jquery post cookie,以便于您获取更多的相关知识。
时间: 2024-10-20 03:28:46