/*

使用方法:
MyHttp loHttp = new MyHttp();
string lcHtml = "";

loHttp.HandleCookies = true;//操作Cookies
loHttp.Method = "GET";
lcHtml = loHttp.GetUrl("
http://signin.ebay.com.cn/ws2/eBayISAPI.dll?SignIn&ssPageName=h:h:sout:CN");

loHttp.AddPostKey("Key", "Value");
loHttp.Referer = "
http://signin.ebay.com.cn/ws2/eBayISAPI.dll?SignIn&ssPageName=h:h:sout:CN";

loHttp.Method = "POST";
lcHtml = loHttp.GetUrl("
http://signin.ebay.com.cn/ws2/eBayISAPI.dll");

MessageBox.Show(loHttp.ErrorMsg);
MessageBox.Show(lcHtml);

*/

using System;
using System.Collections;
using System.Text;
using System.Web;
using System.Windows.Forms;//only For Use MessageBox
using System.Net;
using System.IO;
using System.Diagnostics;

namespace HttpWeb
{
    
public class MyHttp
    {
        
/// <summary>
        
/// User name used for Authentication. 
        
/// To use the currently logged in user when accessing an NTLM resource you can use "AUTOLOGIN".
        
/// </summary>
        public string Username
        {
            
get { return this.cUsername; }
            
set { cUsername = value; }
        }

        
/// <summary>
        
/// Password for Authentication.
        
/// </summary>
        public string Password
        {
            
get { return this.cPassword; }
            
set { this.cPassword = value; }
        }

        
/// <summary>
        
/// Address of the Proxy Server to be used.
        
/// Use optional DEFAULTPROXY value to specify that you want to IE's Proxy Settings
        
/// </summary>
        public string ProxyAddress
        {
            
get { return this.cProxyAddress; }
            
set { this.cProxyAddress = value; }
        }

        
/// <summary>
        
/// Semicolon separated Address list of the servers the proxy is not used for.
        
/// </summary>
        public string ProxyBypass
        {
            
get { return this.cProxyBypass; }
            
set { this.cProxyBypass = value; }
        }

        
/// <summary>
        
/// Username for a password validating Proxy. Only used if the proxy info is set.
        
/// </summary>
        public string ProxyUsername
        {
            
get { return this.cProxyUsername; }
            
set { this.cProxyUsername = value; }
        }
        
/// <summary>
        
/// Password for a password validating Proxy. Only used if the proxy info is set.
        
/// </summary>
        public string ProxyPassword
        {
            
get { return this.cProxyPassword; }
            
set { this.cProxyPassword = value; }
        }

        
/// <summary>
        
/// Timeout for the Web request in seconds. Times out on connection, read and send operations.
        
/// Default is 30 seconds.
        
/// </summary>
        public int Timeout
        {
            
get { return this.nConnectTimeout; }
            
set { this.nConnectTimeout = value; }
        }



        
public bool HandleReferer
        {
            
get { return this.bHandleReferer; }
            
set { this.bHandleReferer = value; }
        }

        
/// <summary>
        
/// 引用页
        
/// </summary>
        public string Referer
        {
            
get { return this.cReferer; }
            
set { this.cReferer = value; }
        }

        
/// <summary>
        
/// 提交模式,默认是POST,用GET模式的时候不能使用PostData
        
/// </summary>
        
/// <value></value>
        public string Method
        {
            
get { return this.cMethod; }
            
set { this.cMethod = value; }
        }
        
/// <summary>
        
/// Error Message if the Error Flag is set or an error value is returned from a method.
        
/// </summary>
        public string ErrorMsg
        {
            
get { return this.cErrorMsg; }
            
set { this.cErrorMsg = value; }
        }

        
/// <summary>
        
/// Error flag if an error occurred.
        
/// </summary>
        public bool Error
        {
            
get { return this.bError; }
            
set { this.bError = value; }
        }

        
/// <summary>
        
/// Determines whether errors cause exceptions to be thrown. By default errors 
        
/// are handled in the class and the Error property is set for error conditions.
        
/// (not implemented at this time).
        
/// </summary>
        public bool ThrowExceptions
        {
            
get { return bThrowExceptions; }
            
set { this.bThrowExceptions = value; }
        }

        
/// <summary>
        
/// If set to a non-zero value will automatically track cookies. The number assigned is the cookie count.
        
/// </summary>
        public bool HandleCookies
        {
            
get { return this.bHandleCookies; }
            
set { this.bHandleCookies = value; }
        }
        
//Cookies集合
        public CookieCollection Cookies
        {
            
get { return this.oCookies; }
            
set { this.oCookies = value; }
        }

        
//默认的编码
        public string MyEncoding
        {
            
get { return this.cEncoding; }
            
set { this.cEncoding = value; }
        }

        
//自动跳转到新的页面
        public bool Location
        {
            
get { return this.bLocation; }
            
set { this.bLocation = value; }
        }
        
// *** member properties
        string cPostData = ""//提交的数据
        int nConnectTimeout = 180//超时
        string cUserAgent = " Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 2.0.50727; .NET CLR 1.1.4322)"//用户代理
        bool bHandleReferer = true//自动操作引用页
        string cReferer = ""//引用页
        string cMethod = "POST"//提交模式POST ro GET
        string cUsername = "";
        
string cPassword = "";
        
string cProxyAddress = "";
        
string cProxyBypass = "";
        
string cProxyUsername = "";
        
string cProxyPassword = "";
        
bool bThrowExceptions = true//是否抛出异常
        bool bHandleCookies = true//自动操作Cookies
        CookieCollection oCookies;
        
string cErrorMsg = ""//错误返回
        bool bError = false;
        
string cEncoding = "GB2312";//UTF-8 GB2312
        bool bLocation = false;

        
public MyHttp() { }

        
/// <summary>
        
/// 增加提交的值
        
/// </summary>
        
/// <param name="Key"></param>
        
/// <param name="Value"></param>
        public void AddPostKey(string Key, string Value)
        {
            cPostData 
+= Key + "=" + System.Web.HttpUtility.UrlEncode(Value, System.Text.Encoding.GetEncoding("GB2312")) + "&";
        }

        
/// <summary>
        
/// 增加提交的连续值(完整或者部分完整值)
        
/// </summary>
        
/// <param name="FullPostBuffer"></param>
        public void AddPostKey(string FullPostBuffer)
        {
            cPostData 
+= FullPostBuffer;
        }







        
public string GetUrl(string Url)
        {
            Url 
= UrlEncode(Url);
            Debug.WriteLine(Url);
            
try
            {
                
this.bError = false;
                
this.cErrorMsg = "";

                
//通用的属性
                HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(Url);
                Request.UserAgent 
= this.cUserAgent;
                Request.Timeout 
= this.nConnectTimeout * 1000;
                Request.Accept 
= "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*";
                Request.Referer 
= this.cReferer;
                
//Request.Connection = "keep-alive";

                
// 需要安全验证的访问
                if (this.cUsername.Length > 0)
                {
                    
if (this.cUsername == "AUTOLOGIN")
                        Request.Credentials 
= CredentialCache.DefaultCredentials;
                    
else
                        Request.Credentials 
= new NetworkCredential(this.cUsername, this.cPassword);
                }

                
// 需要使用Proxy和其配置
                if (this.cProxyAddress.Length > 0)
                {
                    
if (this.cProxyAddress == "DEFAULTPROXY")
                    {
                        Request.Proxy 
= new WebProxy();
                        Request.Proxy 
= WebProxy.GetDefaultProxy();
                    }
                    
else
                    {
                        WebProxy loProxy 
= new WebProxy(this.cProxyAddress, true);
                        
if (this.cProxyBypass.Length > 0)
                        {
                            loProxy.BypassList 
= this.cProxyBypass.Split(';');
                        }

                        
if (this.cProxyUsername.Length > 0)
                            loProxy.Credentials 
= new NetworkCredential(this.cProxyUsername, this.cProxyPassword);

                        Request.Proxy 
= loProxy;
                    }
                }

                
// 需要操作Cookies和自动重用Cookies
                if (this.bHandleCookies)
                {
                    Request.CookieContainer 
= new CookieContainer();
                    
if (this.oCookies != null && this.oCookies.Count > 0)
                    {
                        Request.CookieContainer.Add(
this.oCookies);
                    }
                }

                Request.Method 
= cMethod;//设置提交模式

                
if (this.cMethod == "POST")
                {
                    Request.ContentType 
= "application/x-www-form-urlencoded";
                    
if (this.cPostData.EndsWith("&"))
                        
this.cPostData = this.cPostData.Substring(0this.cPostData.Length - 1);

                    
//MessageBox.Show(this.cPostData);

                    
byte[] lbPostBuffer = System.Text.Encoding.GetEncoding(cEncoding).GetBytes(cPostData);
                    Request.ContentLength 
= lbPostBuffer.Length;
                    Stream loPostData 
= Request.GetRequestStream();

                    loPostData.Write(lbPostBuffer, 
0, lbPostBuffer.Length);
                    loPostData.Close();

                    
// *** clear the POST buffer
                    this.cPostData = "";
                }

                
// *** Retrieve the response headers 
                HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

                
// ** Save cookies the server sends
                if (this.bHandleCookies)
                {
                    
if (Response.Cookies.Count > 0)
                    {
                        
if (this.oCookies == null)
                        {
                            
this.oCookies = Response.Cookies;
                        }
                        
else
                        {
                            
// ** If we already have cookies update the list
                            foreach (Cookie oRespCookie in Response.Cookies)
                            {
                                
bool bMatch = false;
                                
foreach (Cookie oReqCookie in this.oCookies)
                                {
                                    
if (oReqCookie.Name == oRespCookie.Name)
                                    {
                                        oReqCookie.Value 
= oRespCookie.Value;
                                        bMatch 
= true;
                                        
break// 
                                    }
                                } 
// for each ReqCookies
                                if (!bMatch)
                                    
this.oCookies.Add(oRespCookie);
                            } 
// for each Response.Cookies
                        } // this.Cookies == null
                    } // if Response.Cookie.Count > 0
                } // if this.bHandleCookies = 0


                
// *** Save the response object for external access
                Encoding enc;
                
try
                {
                    
if (Response.ContentEncoding.Length > 0)
                        enc 
= Encoding.GetEncoding(Response.ContentEncoding);
                    
else
                        enc 
= Encoding.GetEncoding(cEncoding);
                }
                
catch
                {
                    
// *** Invalid encoding passed
                    enc = Encoding.GetEncoding(cEncoding);
                }

                
// *** drag to a stream
                StreamReader strResponse = new StreamReader(Response.GetResponseStream(), enc);
                
string str = strResponse.ReadToEnd();
                Response.Close();
                strResponse.Close();
                
//自动跟踪引用页
                if (this.bHandleReferer)
                {
                    
this.cReferer = Url;
                }
                
//自动处理HTTP/1.0 302 Moved Temporarily中的Location后的页面。(自动完成跳转)
                if (this.bLocation)
                {

                    
//这里需要自动获得跳转页面的地址。并且再次使用这个方法访问页面
                }
                
return str;
            }
            
catch (Exception e)
            {
                
if (this.bThrowExceptions)
                    
throw e;
                
this.cErrorMsg = e.Message;
                
this.bError = true;
                
return null;
            }
        }
        
private string UrlEncode(string url)
        {
            
byte[] bs = Encoding.GetEncoding("gb2312").GetBytes(url);
            StringBuilder sb 
= new StringBuilder();
            
for (int i = 0; i < bs.Length; i++)
            {
                
if (bs[i] < 128)
                    sb.Append((
char)bs[i]);
                
else
                {
                    sb.Append(
"%" + bs[i++].ToString("x").PadLeft(2'0'));
                    sb.Append(
"%" + bs[i].ToString("x").PadLeft(2'0'));
                }
            }
            
return sb.ToString();
        }


    }
}









第二个通用类:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace HttpWeb
{
    
/// <summary> 
    
///  Http操作类 
    
/// </summary> 
    public static class httptest
    {
        
/// <summary> 
        
///  获取网址HTML 
        
/// </summary> 
        
/// <param name="URL">网址 </param> 
        
/// <returns> </returns> 
        public static string GetHtml(string URL)
        {
            WebRequest wrt;
            wrt 
= WebRequest.Create(URL);
            wrt.Credentials 
= CredentialCache.DefaultCredentials;
            WebResponse wrp;
            wrp 
= wrt.GetResponse();
            
string reader = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding("gb2312")).ReadToEnd();
            
try
            {
                wrt.GetResponse().Close();
            }
            
catch (WebException ex)
            {
                
throw ex;
            }
            
return reader;
        }
        
/// <summary> 
        
/// 获取网站cookie 
        
/// </summary> 
        
/// <param name="URL">网址 </param> 
        
/// <param name="cookie">cookie </param> 
        
/// <returns> </returns> 
        public static string GetHtml(string URL, out string cookie)
        {
            WebRequest wrt;
            wrt 
= WebRequest.Create(URL);
            wrt.Credentials 
= CredentialCache.DefaultCredentials;
            WebResponse wrp;

            wrp 
= wrt.GetResponse();


            
string html = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding("gb2312")).ReadToEnd();

            
try
            {
                wrt.GetResponse().Close();
            }
            
catch (WebException ex)
            {
                
throw ex;
            }

            cookie 
= wrp.Headers.Get("Set-Cookie");
            
return html;
        }
        
public static string GetHtml(string URL, string postData, string cookie, out string header, string server)
        {
            
return GetHtml(server, URL, postData, cookie, out header);
        }
        
public static string GetHtml(string server, string URL, string postData, string cookie, out string header)
        {
            
byte[] byteRequest = Encoding.GetEncoding("gb2312").GetBytes(postData);
            
return GetHtml(server, URL, byteRequest, cookie, out header);
        }
        
public static string GetHtml(string server, string URL, byte[] byteRequest, string cookie, out string header)
        {
            
byte[] bytes = GetHtmlByBytes(server, URL, byteRequest, cookie, out header);
            Stream getStream 
= new MemoryStream(bytes);
            StreamReader streamReader 
= new StreamReader(getStream, Encoding.GetEncoding("gb2312"));
            
string getString = streamReader.ReadToEnd();
            streamReader.Close();
            getStream.Close();
            
return getString;
        }

        
/// <summary> 
        
/// Post模式浏览 
        
/// </summary> 
        
/// <param name="server">服务器地址 </param> 
        
/// <param name="URL">网址 </param> 
        
/// <param name="byteRequest">流 </param> 
        
/// <param name="cookie">cookie </param> 
        
/// <param name="header">句柄 </param> 
        
/// <returns> </returns> 
        public static byte[] GetHtmlByBytes(string server, string URL, byte[] byteRequest, string cookie, out string header)
        {
            
long contentLength;
            HttpWebRequest httpWebRequest;
            HttpWebResponse webResponse;
            Stream getStream;

            httpWebRequest 
= (HttpWebRequest)HttpWebRequest.Create(URL);
            CookieContainer co 
= new CookieContainer();
            co.SetCookies(
new Uri(server), cookie);

            httpWebRequest.CookieContainer 
= co;

            httpWebRequest.ContentType 
= "application/x-www-form-urlencoded";
            httpWebRequest.Accept 
=
                
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            httpWebRequest.Referer 
= server;
            httpWebRequest.UserAgent 
=
                
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
            httpWebRequest.Method 
= "Post";
            httpWebRequest.ContentLength 
= byteRequest.Length;
            Stream stream;
            stream 
= httpWebRequest.GetRequestStream();
            stream.Write(byteRequest, 
0, byteRequest.Length);
            stream.Close();
            webResponse 
= (HttpWebResponse)httpWebRequest.GetResponse();
            header 
= webResponse.Headers.ToString();
            getStream 
= webResponse.GetResponseStream();
            contentLength 
= webResponse.ContentLength;

            
byte[] outBytes = new byte[contentLength];
            outBytes 
= ReadFully(getStream);
            getStream.Close();
            
return outBytes;
        }
        
public static byte[] ReadFully(Stream stream)
        {
            
byte[] buffer = new byte[128];
            
using (MemoryStream ms = new MemoryStream())
            {
                
while (true)
                {
                    
int read = stream.Read(buffer, 0, buffer.Length);
                    
if (read <= 0)
                        
return ms.ToArray();
                    ms.Write(buffer, 
0, read);
                }
            }
        }

        
/// <summary> 
        
/// Get模式 
        
/// </summary> 
        
/// <param name="URL">网址 </param> 
        
/// <param name="cookie">cookies </param> 
        
/// <param name="header">句柄 </param> 
        
/// <param name="server">服务器 </param> 
        
/// <param name="val">服务器 </param> 
        
/// <returns> </returns> 
        public static string GetHtml(string URL, string cookie, out string header, string server)
        {
            
return GetHtml(URL, cookie, out header, server, "");
        }
        
/// <summary> 
        
/// Get模式浏览 
        
/// </summary> 
        
/// <param name="URL">Get网址 </param> 
        
/// <param name="cookie">cookie </param> 
        
/// <param name="header">句柄 </param> 
        
/// <param name="server">服务器地址 </param> 
        
/// <param name="val"> </param> 
        
/// <returns> </returns> 
        public static string GetHtml(string URL, string cookie, out string header, string server, string val)
        {
            HttpWebRequest httpWebRequest;
            HttpWebResponse webResponse;
            Stream getStream;
            StreamReader streamReader;
            
string getString = "";
            httpWebRequest 
= (HttpWebRequest)HttpWebRequest.Create(URL);
            httpWebRequest.Accept 
= "*/*";
            httpWebRequest.Referer 
= server;
            CookieContainer co 
= new CookieContainer();
            co.SetCookies(
new Uri(server), cookie);
            httpWebRequest.CookieContainer 
= co;
            httpWebRequest.UserAgent 
=
                
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
            httpWebRequest.Method 
= "GET";
            webResponse 
= (HttpWebResponse)httpWebRequest.GetResponse();
            header 
= webResponse.Headers.ToString();
            getStream 
= webResponse.GetResponseStream();
            streamReader 
= new StreamReader(getStream, Encoding.GetEncoding("gb2312"));
            getString 
= streamReader.ReadToEnd();

            streamReader.Close();
            getStream.Close();
            
return getString;
        }
        
/// <summary> 
        
/// 返回验证码图片流 
        
/// </summary> 
        
/// <param name="server">服务器地址 </param> 
        
/// <param name="URL">验证码网址 </param> 
        
/// <param name="cookie">cookie </param> 
        
/// <returns> </returns> 
        public static Stream GetStreamByBytes(string server, string URL, string cookie)
        {
            Stream stream 
= GetCode(server, URL, cookie);
            
return stream;
        }

        
/// <summary> 
        
/// //获取验证码 
        
/// </summary> 
        
/// <param name="server">服务器地址 </param> 
        
/// <param name="url">验证码网址 </param> 
        
/// <param name="cookie">cookie </param> 
        
/// <returns> </returns> 
        public static Stream GetCode(string server, string url, string cookie)
        {

            HttpWebRequest httpWebRequest;

            httpWebRequest 
= (HttpWebRequest)HttpWebRequest.Create(url);
            CookieContainer co 
= new CookieContainer();
            co.SetCookies(
new Uri(server), cookie);
            httpWebRequest.CookieContainer 
= co;

            HttpWebResponse response 
= (HttpWebResponse)httpWebRequest.GetResponse();
            Stream stream 
= response.GetResponseStream();
            
return stream;

        }
        
/// <summary> 
        
/// 获取html 
        
/// </summary> 
        
/// <param name="server"> </param> 
        
/// <param name="url"> </param> 
        
/// <param name="cookie"> </param> 
        
/// <returns> </returns> 

        
public static string GetUser(string server, string url, string cookie)
        {
            
string getString = "";
            
try
            {
                HttpWebRequest httpWebRequest;
                StreamReader streamReader;


                httpWebRequest 
= (HttpWebRequest)HttpWebRequest.Create(url);
                CookieContainer co 
= new CookieContainer();
                co.SetCookies(
new Uri(server), cookie);
                httpWebRequest.CookieContainer 
= co;

                HttpWebResponse response 
= (HttpWebResponse)httpWebRequest.GetResponse();

                Stream stream 
= response.GetResponseStream();


                streamReader 
= new StreamReader(stream, Encoding.GetEncoding("gb2312"));
                getString 
= streamReader.ReadToEnd();


                
try
                {
                    httpWebRequest.GetResponse().Close();
                }
                
catch (WebException ex)
                {
                    
throw ex;
                }
                streamReader.Close();
                stream.Close();

            }
            
catch
            {
            }
            
return getString;

        }

    }

}