程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# HttpWebRequest通用類(一)

C# HttpWebRequest通用類(一)

編輯:C#入門知識

/*

使用方法:
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(0, this.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();
        }


    }
}

  作者“RiXu Blog (日需博客)”
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved