欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#模擬Http與Https請求框架類實(shí)例

 更新時(shí)間:2014年12月27日 10:44:52   投稿:shichen2014  
這篇文章主要介紹了C#模擬Http與Https請求框架類,實(shí)例分析了處理http與https請求的方法與信息處理的技巧,需要的朋友可以參考下

本文實(shí)例講述了C#模擬Http與Https請求框架類。分享給大家供大家參考。

具體實(shí)現(xiàn)方法如下:

復(fù)制代碼 代碼如下:

using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

namespace WebRequestTest
{
    /// <summary>
    /// 動(dòng)態(tài)類,每個(gè)實(shí)例使用單獨(dú)session
    /// </summary>
    public class HttpHelperNew
    {
       public CookieContainer cookie = new CookieContainer();
        /// <summary>
        /// post請求返回html
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="postDataStr"></param>
        /// <returns></returns>
        public string HttpPost(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            //request.AllowAutoRedirect = false; //禁止自動(dòng)重定向
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
            request.CookieContainer = cookie; //cookie信息由CookieContainer自行維護(hù)
            Stream myRequestStream = request.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
            myStreamWriter.Write(postDataStr);
            myStreamWriter.Close();

            HttpWebResponse response = null;
            try
            {
                this.SetCertificatePolicy();
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (System.Exception ex)
            {
            }
            //獲取重定向地址
            //string url1 = response.Headers["Location"];
            if (response !=null)
            {
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;
            }
            else
            {
                return "error"; //post請求返回為空
            }
        }

        /// <summary>
        /// get請求獲取返回的html
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="postDataStr"></param>
        /// <returns></returns>
        public string HttpGet(string Url, string Querydata)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (Querydata == "" ? "" : "?") + Querydata);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            request.CookieContainer = cookie;
            this.SetCertificatePolicy();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
           // response.Cookies = cookie.GetCookies(response.ResponseUri);
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }

        /// <summary>
        /// 獲得響應(yīng)中的圖像
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public Stream GetResponseImage(string url)
        {
            Stream resst = null;
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.KeepAlive = true;
                req.Method = "GET";
                req.AllowAutoRedirect = true;
                req.CookieContainer = cookie;
                req.ContentType = "application/x-www-form-urlencoded";
                req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                req.Timeout = 50000;

                Encoding myEncoding = Encoding.GetEncoding("UTF-8");
                this.SetCertificatePolicy();
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                resst = res.GetResponseStream();

                return resst;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 正則獲取匹配的第一個(gè)值
        /// </summary>
        /// <param name="html"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public string getStringByRegex(string html,string pattern)
        {

            Regex re = new Regex(pattern, RegexOptions.IgnoreCase);
            MatchCollection matchs = re.Matches(html);
            if (matchs.Count > 0)
            {
                return matchs[0].Groups[1].Value;
            }
            else
                return "";
        }

        /// <summary>
        /// 正則驗(yàn)證返回的response是否正確
        /// </summary>
        /// <param name="html"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public bool verifyResponseHtml(string html ,string pattern)
        {
            Regex re = new Regex(pattern);
            return re.IsMatch(html);
        }

        //注冊證書驗(yàn)證回調(diào)事件,在請求之前注冊
        private void SetCertificatePolicy()
        {
            ServicePointManager.ServerCertificateValidationCallback
                       += RemoteCertificateValidate;
        }
        /// <summary> 
        /// 遠(yuǎn)程證書驗(yàn)證,固定返回true
        /// </summary> 
        private static bool RemoteCertificateValidate(object sender, X509Certificate cert,
            X509Chain chain, SslPolicyErrors error)
        {
            return true;
        } 
    }
}

希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • C#多線程系列之多線程鎖lock和Monitor

    C#多線程系列之多線程鎖lock和Monitor

    這篇文章介紹了C#多線程鎖lock和Monitor的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • 關(guān)于C#結(jié)構(gòu)體 你需要知道的

    關(guān)于C#結(jié)構(gòu)體 你需要知道的

    這篇文章主要介紹了關(guān)于C#結(jié)構(gòu)體的相關(guān)知識,以及使用方法,文中代碼非常詳細(xì),幫助大家更好的參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • c# socket網(wǎng)絡(luò)編程接收發(fā)送數(shù)據(jù)示例代碼

    c# socket網(wǎng)絡(luò)編程接收發(fā)送數(shù)據(jù)示例代碼

    這篇文章主要介紹了c# socket網(wǎng)絡(luò)編程,server端接收,client端發(fā)送數(shù)據(jù),大家參考使用吧
    2013-12-12
  • c# 獲得本地ip地址的三種方法

    c# 獲得本地ip地址的三種方法

    這篇文章主要介紹了c# 獲得本地ip地址的三種方法,幫助大家更好的理解和實(shí)用c#,感興趣的朋友可以了解下
    2020-12-12
  • c# 匿名方法的小例子

    c# 匿名方法的小例子

    c# 匿名方法的小例子,需要的朋友可以參考一下
    2013-04-04
  • C#算法之回文數(shù)

    C#算法之回文數(shù)

    這篇文章介紹了C#算法之回文數(shù),文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01
  • LINQ基礎(chǔ)之Join和UNION子句

    LINQ基礎(chǔ)之Join和UNION子句

    這篇文章介紹了LINQ使用Join和UNION子句的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#實(shí)現(xiàn)將聊天數(shù)據(jù)發(fā)送加密

    C#實(shí)現(xiàn)將聊天數(shù)據(jù)發(fā)送加密

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)將聊天數(shù)據(jù)發(fā)送加密的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • C#透明窗體實(shí)現(xiàn)方法

    C#透明窗體實(shí)現(xiàn)方法

    這篇文章主要介紹了C#透明窗體實(shí)現(xiàn)方法,涉及C#窗體操作的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • C#模擬http 發(fā)送post或get請求的簡單實(shí)例

    C#模擬http 發(fā)送post或get請求的簡單實(shí)例

    下面小編就為大家?guī)硪黄狢#模擬http 發(fā)送post或get請求的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-06-06

最新評論