Пример #1
0
        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="url">网址</param>
        /// <param name="postData">账号参数</param>
        /// <param name="httpParas">协议头</param>
        /// <param name="encoding">网站支持的编码格式</param>
        /// <returns></returns>
        public string PostHtml(string url, string postData, HttpPara httpParas, Encoding encoding)
        {
            byte[] bt = encoding.GetBytes(postData);

            if (!url.Contains("http://"))
            {
                url = "http://" + url;
            }
            HttpWebRequest req = WebRequest.Create(new Uri(url)) as HttpWebRequest;

            //协议头的赋值
            req.Method      = "POST";
            req.Referer     = httpParas.Referer;
            req.Accept      = httpParas.Accept;
            req.ContentType = httpParas.ContentType;
            req.UserAgent   = httpParas.UserAgent;

            using (Stream streamReq = req.GetRequestStream())
            {
                streamReq.Write(bt, 0, bt.Length);
                HttpWebResponse res = req.GetResponse() as HttpWebResponse;
                using (Stream streamRes = res.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(streamRes, encoding))
                    {
                        string result = reader.ReadToEnd();
                        return(result);
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Get请求
        /// </summary>
        /// <param name="url">网址</param>
        /// <param name="httpParas">协议头</param>
        /// <param name="encoding">网站支持的编码格式</param>
        /// <returns></returns>
        public string GetHtml(string url, HttpPara httpParas, Encoding encoding)
        {
            if (!url.Contains("http://"))
            {
                url = "http://" + url;
            }
            HttpWebRequest req = WebRequest.Create(new Uri(url)) as HttpWebRequest;

            //协议头的赋值
            req.Method      = "GET";
            req.Referer     = httpParas.Referer;
            req.Accept      = httpParas.Accept;
            req.ContentType = httpParas.ContentType;
            req.UserAgent   = httpParas.UserAgent;

            HttpWebResponse res = req.GetResponse() as HttpWebResponse;

            using (Stream stream = res.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream, encoding))
                {
                    string result = reader.ReadToEnd();
                    return(result);
                }
            }
        }