서버post 또는 get 데이터로 돌아가기

10274 단어 post
        #region  Get 
        /// <summary>
        ///  Get 
        /// </summary>
        ///<see cref="Author"> </see> 
        /// <param name="url"></param>
        /// <param name="param"></param>
        /// <param name="param">GET</param>
        /// <returns></returns>
        public static string SendInformation(string Url, string Param, string Method = "GET")
        {           
            if (!string.IsNullOrEmpty(Param))
            {
                Url = Url + "?" + Param;                
            }
            Uri uri = new Uri(Url);
            HttpWebRequest webReq = HttpWebRequest.Create(uri) as System.Net.HttpWebRequest;
            webReq.Method = Method;
            webReq.Headers["Cache-Control"] = "no-cache";
            HttpWebResponse webResp = null;
            Stream respStream = null;
            StreamReader objReader = null;
            try
            {
                webResp = (HttpWebResponse)webReq.GetResponse();
                respStream = webResp.GetResponseStream();
                objReader = new StreamReader(respStream, System.Text.Encoding.GetEncoding("UTF-8"));
                return objReader.ReadToEnd();
            }
            catch (WebException ex)
            {
                return " /r/n :" + ex.Message;
            }
            finally
            {
                if (webResp != null)
                    webResp.Close();
                if (respStream != null)
                    respStream.Close();
                if (objReader != null)
                    objReader.Close();
            }
        }
        #endregion

        #region  post 
        /// <summary>
        ///  post 
        /// </summary>
        /// <param name="url"> URL</param>
        /// <param name="param"> </param>
        /// <returns> </returns>
        public static string PostInformation(string url, string param, string Method="Post")
        {
            System.Text.Encoding myEncode = System.Text.Encoding.GetEncoding("UTF-8");
            byte[] postBytes = System.Text.Encoding.ASCII.GetBytes(param);
            HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(url);
            webReq.Headers["Cache-Control"] = "no-cache";
            webReq.Method = Method;
            webReq.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            webReq.ContentLength = postBytes.Length;
            Stream reqStream = null;
            HttpWebResponse webResp = null;
            StreamReader sr = null;
            try
            {
                reqStream = webReq.GetRequestStream();
                reqStream.Write(postBytes, 0, postBytes.Length);
                webResp = (HttpWebResponse)webReq.GetResponse();
                sr = new StreamReader(webResp.GetResponseStream(), myEncode);
                return sr.ReadToEnd();
            }
            catch (WebException ex)
            {
                return " \r
" + ex.Message; } finally { if (reqStream != null) reqStream.Close(); if (webResp != null) webResp.Close(); if (sr != null) sr.Close(); } } #endregion

좋은 웹페이지 즐겨찾기