向服务器post或者get数据返回

        #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\n错误信息:" + ex.Message;
            }
            finally
            {
                if (reqStream != null)
                    reqStream.Close();
                if (webResp != null)
                    webResp.Close();
                if (sr != null)
                    sr.Close();
            }
        }
        #endregion

 

你可能感兴趣的:(post)