C# Winform 通用 Httprequest 类

On 2009年04月12日, in .Net, by Kane

post 和 get 都可以。

 private static string PGetHtml(string url, string postData, Encoding enc, CookieContainer cookie)
        {
            //返回的html
            string html = String.Empty;

            bool m_UsePost = true;

            if (postData == null || postData.Length == 0)
                m_UsePost = false;

            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727; Alexa Toolbar)";
                req.Method = m_UsePost ? "POST" : "GET";
                req.Referer = url;
                req.ContentType = "application/x-www-form-urlencoded";
                req.KeepAlive = true;
                req.Accept = "*/*";

                if (cookie != null)
                {
                    req.CookieContainer = cookie;
                }

                if (m_UsePost)
                {
                    req.ContentLength = postData.Length;
                    using (StreamWriter writer = new StreamWriter(req.GetRequestStream()))
                    {
                        writer.Write(postData);
                        writer.Close();
                    }
                }

                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                if (cookie != null)
                {
                    resp.Cookies = cookie.GetCookies(new Uri(url));
                }

                StreamReader sr = new StreamReader(resp.GetResponseStream(), enc);
                html = sr.ReadToEnd();
                sr.Close();
                resp.Close();
            }
            catch (Exception ex)
            {
                html = "";
                MarkLog(ex.ToString(), "**系统错误**-sic");
            }

            return html;
        }

request 之后对返回的内容进行查找。

public static string GetBetweenText(string sourceStr, string startStr, string endStr)
        {
            string result = string.Empty;
            int startIdx = 0;
            int endIdx = 0;
            startIdx = sourceStr.IndexOf(startStr);
            if (startIdx >= 0)
            {
                endIdx = sourceStr.IndexOf(endStr, startIdx);
                if (endIdx > startIdx && endIdx > 0)
                {
                    result = sourceStr.Substring(startIdx + startStr.Length, endIdx - startIdx - startStr.Length);
                }
            }
            return result;

        }

简单 Marllog

public static void MarkLog(string msg, string type)
        {

            string Logfile = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\log_" + DateTime.Now.ToString("yyyy_MM_dd") + ".txt";
            FileStream fs = new FileStream(Logfile, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);
            sw.WriteLine(string.Format("{0}:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "["+type+"]" +msg));
            sw.Close();
            fs.Close();
            Console.WriteLine(string.Format("{0}:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "["+type+"]" +msg));

        }
 

Leave a Reply