C# HttpHelper帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取

// 取当前webBrowser登录后的Cookie值
        [DllImport( " wininet.dll ", CharSet = CharSet.Auto, SetLastError =  true)]
         static  extern  bool InternetGetCookieEx( string pchURL,  string pchCookieName, StringBuilder pchCookieData,  ref  int pcchCookieData,  int dwFlags,  object lpReserved);

         // 取出Cookie,当登录后才能取
         private  static  string GetCookieString( string url)
        {
             //  Determine the size of the cookie     
             int datasize =  256;
            StringBuilder cookieData =  new StringBuilder(datasize);
             if (!InternetGetCookieEx(url,  null, cookieData,  ref datasize,  0x00002000null))
            {
                 if (datasize <  0)
                     return  null;
                 //  Allocate stringbuilder large enough to hold the cookie     
                cookieData =  new StringBuilder(datasize);
                 if (!InternetGetCookieEx(url,  null, cookieData,  ref datasize,  0x00002000null))
                     return  null;
            }
             return cookieData.ToString();
        }之前我写过篇关于C# HttpWebRequest 绝技的文章 无视证书的方法请在篇文章里面查找谢谢,以下不做无视证书的方法

无论是Get还是Post 带Cookie,编码问题解决 ,这里完全做到了无视编码的问题

最近把上面的方法整理了一下,这个类相当于一个SqlHelper类一下,我把它叫做HttpHelper类,以后还会慢慢更新,希望大家多我支持,

给大家分享一下吧,不好的地方感谢大家留言指正,不多说了上代码吧!

///   <summary>
///  类说明:HttpHelps类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式
///  编码日期:2011-08-20
///   </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.IO.Compression;

public  class HttpHelps
{
     #region 预定义方法或者变更

     // 默认的编码
     private Encoding encoding = Encoding.Default;
     // HttpWebRequest对象用来发起请求
     private HttpWebRequest request =  null;
     // 获取影响流的数据对象
     private HttpWebResponse response =  null;
     // 读取流的对象
     private StreamReader reader =  null;
     // 需要返回的数据对象
     private  string returnData =  " String Error ";

     ///   <summary>
    
///  根据相传入的数据,得到相应页面数据
    
///   </summary>
    
///   <param name="strPostdata"> 传入的数据Post方式,get方式传NUll或者空字符串都可以 </param>
    
///   <returns> string类型的响应数据 </returns>
     private  string GetHttpRequestData( string strPostdata)
    {
         try
        {
             // 支持跳转页面,查询结果将是跳转后的页面
            request.AllowAutoRedirect =  true;

             // 验证在得到结果时是否有传入数据
             if (! string.IsNullOrEmpty(strPostdata) && request.Method.Trim().ToLower().Contains( " post "))
            {
                 byte[] buffer = encoding.GetBytes(strPostdata);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer,  0, buffer.Length);
            }

             #region 得到请求的response

             using (response = (HttpWebResponse)request.GetResponse())
            {
                 // 从这里开始我们要无视编码了
                 if (encoding ==  null)
                {
                    MemoryStream _stream =  new MemoryStream();

                     if (response.ContentEncoding !=  null && response.ContentEncoding.Equals( " gzip ", StringComparison.InvariantCultureIgnoreCase))
                    {
                         // 开始读取流并设置编码方式
                         new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream,  10240);
                    }
                     else
                    {
                        response.GetResponseStream().CopyTo(_stream,  10240);
                    }
                     byte[] RawResponse = _stream.ToArray();
                     string temp = Encoding.Default.GetString(RawResponse,  0, RawResponse.Length);
                     // <meta(.*?)charset([\s]?)=[^>](.*?)>
                    Match meta = Regex.Match(temp,  " <meta([^<]*)charset=([^<]*)[\"'] ", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                     string charter = (meta.Groups.Count >  2) ? meta.Groups[ 2].Value :  string.Empty;
                    charter = charter.Replace( " \" "string.Empty).Replace( " ' "string.Empty).Replace( " ; "string.Empty);
                     if (charter.Length >  0)
                    {
                        encoding = Encoding.GetEncoding(charter);
                    }
                     else
                    {
                         if (response.CharacterSet.ToLower().Trim() ==  " iso-8859-1 ")
                        {
                            encoding = Encoding.GetEncoding( " gbk ");
                        }
                         else
                        {
                             if ( string.IsNullOrEmpty(response.CharacterSet.Trim()))
                            {
                                encoding = Encoding.UTF8;
                            }
                             else
                            {
encoding = Encoding.GetEncoding(response.CharacterSet);
                            }
                        }
                    }
                    returnData = encoding.GetString(RawResponse);
                }
                 else
                {
                     if (response.ContentEncoding !=  null && response.ContentEncoding.Equals( " gzip ", StringComparison.InvariantCultureIgnoreCase))
                    {
                         // 开始读取流并设置编码方式
                         using (reader =  new StreamReader( new GZipStream(response.GetResponseStream(), CompressionMode.Decompress), encoding))
                        {
                            returnData = reader.ReadToEnd();
                        }
                    }
                     else
                    {
                         // 开始读取流并设置编码方式
                         using (reader =  new StreamReader(response.GetResponseStream(), encoding))
                        {
                            returnData = reader.ReadToEnd();
                        }
                    }
                }
            }

             #endregion
        }
         catch (Exception)
        {
             // 这里是在发生异常时返回的错误信息
            returnData =  " String Error ";
        }
         return returnData.ToLower();
    }


你可能感兴趣的:(request)