SoFunction
Updated on 2025-03-08

C# simulates Http and Https request framework class instances


using ;
using ;
using ;
using ;
using .X509Certificates;
using ;

namespace WebRequestTest
{
    /// <summary>
/// Dynamic class, each instance uses a separate session
    /// </summary>
    public class HttpHelperNew
    {
       public CookieContainer cookie = new CookieContainer();
        /// <summary>
/// The post request returns html
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="postDataStr"></param>
        /// <returns></returns>
        public string HttpPost(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)(Url);
// = false; // Automatic redirection is prohibited
            = "POST";
            = "application/x-www-form-urlencoded";
            = Encoding.(postDataStr);
= cookie; //Cookie information is maintained by CookieContainer
            Stream myRequestStream = ();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, ("gb2312"));
            (postDataStr);
            ();

            HttpWebResponse response = null;
            try
            {
                ();
                response = (HttpWebResponse)();
            }
            catch ( ex)
            {
            }
//Get the redirect address
            //string url1 = ["Location"];
            if (response !=null)
            {
                Stream myResponseStream = ();
                StreamReader myStreamReader = new StreamReader(myResponseStream, ("utf-8"));
                string retString = ();
                ();
                ();
                return retString;
            }
            else
            {
return "error"; //post request returns empty
            }
        }

        /// <summary>
/// get request to get the returned html
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="postDataStr"></param>
        /// <returns></returns>
        public string HttpGet(string Url, string Querydata)
        {
            HttpWebRequest request = (HttpWebRequest)(Url + (Querydata == "" ? "" : "?") + Querydata);
            = "GET";
            = "text/html;charset=UTF-8";
            = cookie;
            ();
            HttpWebResponse response = (HttpWebResponse)();
           // = ();
            Stream myResponseStream = ();
            StreamReader myStreamReader = new StreamReader(myResponseStream, ("utf-8"));
            string retString = ();
            ();
            ();
            return retString;
        }

        /// <summary>
/// Get the image in the response
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public Stream GetResponseImage(string url)
        {
            Stream resst = null;
            try
            {
                HttpWebRequest req = (HttpWebRequest)(url);
                = true;
                = "GET";
                = true;
                = cookie;
                = "application/x-www-form-urlencoded";
                = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                = 50000;

                Encoding myEncoding = ("UTF-8");
                ();
                HttpWebResponse res = (HttpWebResponse)();
                resst = ();

                return resst;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
/// Regularly get the first matching value
        /// </summary>
        /// <param name="html"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public string getStringByRegex(string html,string pattern)
        {

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

        /// <summary>
/// Regular verification: The response returned is correct
        /// </summary>
        /// <param name="html"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public bool verifyResponseHtml(string html ,string pattern)
        {
            Regex re = new Regex(pattern);
            return (html);
        }

//Register certificate verification callback event, register before request
        private void SetCertificatePolicy()
        {
           
                       += RemoteCertificateValidate;
        }
        /// <summary> 
///Remote certificate verification, fixed return true
        /// </summary> 
        private static bool RemoteCertificateValidate(object sender, X509Certificate cert,
            X509Chain chain, SslPolicyErrors error)
        {
            return true;
        } 
    }
}