Http GET,Post 방식 의 요청 요약

20418 단어 http
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","요청 텍스트");

좋은 웹페이지 즐겨찾기