Http GET、Post方式的请求总结

读取http响应信息,并返回响应体

        /// <summary>

        /// 读取http响应信息,并返回响应体

        /// </summary>

        /// <param name="response">HttpWebResponse对象</param>

        /// <returns></returns>

        public static string ReaderFromHttp(HttpWebResponse response)

        {

            Stream stream = null;

            StreamReader streamReader = null;

            try

            {

                var contentEncoding = response.ContentEncoding; //压缩标志

                var characterSet = response.CharacterSet;       //字符集



                stream = response.GetResponseStream();

                if (contentEncoding == "gzip")

                {

                    /*压缩数据,要进行处理*/

                    throw new NotImplementedException("gzip压缩数据处理还么实现");

                }

                else

                {

                    streamReader = new StreamReader(stream, Encoding.GetEncoding(characterSet));

                    return streamReader.ReadToEnd();

                }



            }

            catch (Exception e)

            {

                return null;

            }

            finally

            {

                if (streamReader != null)

                {

                    streamReader.Close();

                    streamReader = null;

                }

                if (stream != null)

                {

                    stream.Close();

                    stream = null;

                }

            }

        }
View Code

Http  Get 

        #region  Get Http



        /// <summary>

        /// GET方式的HTTP请求

        /// </summary>

        /// <param name="targetUri"></param>

        /// <param name="timeout">超时时间</param>

        /// <param name="cookie">随同HTTP请求发送的Cookie信息</param>

        /// <returns></returns>

        public static HttpWebRequest CreateGetHttpWebRequest(string targetUri, int timeout = 0, CookieCollection cookie = null)

        {

            HttpWebRequest request = WebRequest.Create(targetUri) as HttpWebRequest;

            request.UserAgent = DefaultUserAgent;

            request.Method = "GET";



            if (timeout != 0)

                request.Timeout = timeout;

            if (cookie != null)

            {

                request.CookieContainer = new CookieContainer();

                request.CookieContainer.Add(cookie);

            }



            return request;

        }



        /// <summary>

        /// GET方式的HTTP请求,返回响应体

        /// </summary>

        /// <param name="targetUri"></param>

        /// <param name="timeout">超时时间</param>

        /// <param name="cookie">随同HTTP请求发送的Cookie信息</param>

        /// <returns></returns>

        public static string ReturnFromGetHttp(string targetUri, int timeout = 0, CookieCollection cookie = null)

        {

            HttpWebRequest request = null;

            HttpWebResponse response = null;

            try

            {

                request = CreateGetHttpWebRequest(targetUri, timeout, cookie);

                response = request.GetResponse() as HttpWebResponse;



                return ReaderFromHttp(response);

            }

            catch (Exception e)

            {

                return null;

            }

            finally

            {

                if (response != null)

                {

                    response.Close();

                    response = null;

                }

                if (request != null)

                {

                    request.Abort();

                    request = null;

                }

            }

        }

       

        #endregion
View Code

 

Http Post

        #region  Post Http



        /// <summary>

        /// POST方式的HTTP请求

        /// </summary>

        /// <param name="targetUri"></param>

        /// <param name="content">请求文本信息</param>

        /// <param name="characterSet">字符集(默认utf-8)</param>

        /// <param name="timeout">超时时间</param>

        /// <param name="cookie">随同HTTP请求发送的Cookie信息</param>

        /// <returns></returns>

        public static HttpWebRequest CreatePostHttpWebRequest(string targetUri, string content, string characterSet = null, int timeout = 0, CookieCollection cookie = null)

        {

            HttpWebRequest request = WebRequest.Create(targetUri) as HttpWebRequest;

            request.UserAgent = DefaultUserAgent;

            request.Method = "POST";

            request.ContentType = "application/x-www-form-urlencoded";

            request.Headers.Add("Accept-Encoding", "gzip, deflate");



            if (timeout != 0)

                request.Timeout = timeout;

            if (cookie != null)

            {

                request.CookieContainer = new CookieContainer();

                request.CookieContainer.Add(cookie);

            }



            using (var stream = request.GetRequestStream())  //写入请求数据流

            {

                characterSet = string.IsNullOrEmpty(characterSet) ? "utf-8" : characterSet;

                var bytes = Encoding.GetEncoding(characterSet).GetBytes(content);

                request.ContentLength = bytes.LongLength;

                stream.Write(bytes, 0, bytes.Length);

            }



            return request;

        }



        /// <summary>

        /// POST方式的HTTP请求,返回响应体

        /// </summary>

        /// <param name="targetUri"></param>

        /// <param name="content">请求文本信息</param>

        /// <param name="characterSet">字符集(默认utf-8)</param>

        /// <param name="timeout">超时时间</param>

        /// <param name="cookie">随同HTTP请求发送的Cookie信息</param>

        /// <returns></returns>

        public static string ReturnFromPostHttp(string targetUri, string content, string characterSet = null, int timeout = 0, CookieCollection cookie = null)

        {

            HttpWebRequest request = null;

            HttpWebResponse response = null;

            try

            {

                request = CreatePostHttpWebRequest(targetUri,content,characterSet, timeout, cookie);

                response = request.GetResponse() as HttpWebResponse;



                return ReaderFromHttp(response);

            }

            catch (Exception e)

            {

                return null;

            }

            finally

            {

                if (response != null)

                {

                    response.Close();

                    response = null;

                }

                if (request != null)

                {

                    request.Abort();

                    request = null;

                }

            }

        }

       

        #endregion
View Code

 

调用方式:

var reval = ReturnFromGetHttp("http://www.douyou100.com");
var reval1 = ReturnFromPostHttp("http://www.douyou100.com","请求文本");

你可能感兴趣的:(http)