C# POST/GET

C# Post/Get 一个demo记录一下。

    class HttpManage
    {
        /// <summary>
        /// POST请求
        /// </summary>
        /// <param name="postURL">请求URL</param>
        /// <param name="jsonParam">请求参数JSON格式</param>
        /// <returns></returns>
        public static string POST(string postURL, string jsonParam)
        {
            System.Net.ServicePointManager.DefaultConnectionLimit = 50;

            string result = string.Empty;
            string serviceAddress = postURL;

            HttpWebRequest request = null;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(serviceAddress);
                request.Method = "POST";
                request.ContentType = "application/json";
                //request.ContentType = " application/x-www-form-urlencoded";   //内容类型
                request.ReadWriteTimeout = 30 * 1000;

                string strContent = jsonParam;
                //using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
                //{
                //    dataStream.Write(strContent);
                //    dataStream.Close();
                //}
                byte[] data = Encoding.UTF8.GetBytes(strContent);
                Stream myRequestStream = request.GetRequestStream();
                myRequestStream.Write(data, 0, data.Length);
                myRequestStream.Close();

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    string encoding = response.ContentEncoding;
                    if (encoding == null || encoding.Length < 1)
                    {
                        encoding = "UTF-8"; //默认编码  
                    }
                    StreamReader reader = null;
                    reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                    result = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                //异常,写日志
                Log4NetHelper.WriteErrorLog("POST异常",ex);
            }
            finally
            {
                if (request != null)
                {
                    request.Abort();
                }
            }
            return result;
        }

        public static string PostFromHttpClient(string url,string jsonParam)
        {
            string result = string.Empty;
            return result;
        }

        /// <summary>
        /// GET请求
        /// </summary>
        /// <param name="url">请求URL</param>
        /// <returns></returns>
        public static string GET(string url)
        {
            string result = string.Empty;

            HttpWebRequest request = null;
            try
            {
                request = WebRequest.Create(url) as HttpWebRequest;  //根据用户请求的RUL创建WEB访问对象
                //定义请求方法和标识
                request.Method = "GET";
                request.ContentType = "application/json";
                //request.ContentType = "text/html;charset=UTF-8";
//request.ContentType = " application/x-www-form-urlencoded"; //接收请求 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) //获取响应请求 { //以流的形式读取数据 StreamReader sr = new StreamReader(response.GetResponseStream()); result = sr.ReadToEnd(); sr.Close(); } } catch(Exception ex) { result = string.Empty; Log4NetHelper.WriteErrorLog("GET异常",ex); } finally { if (request != null) { request.Abort(); } } return result; }

 

欢迎相互交流学习!

C# POST/GET

上一篇:C# 使用GDI绘制雷达图


下一篇:delphi客户端自动重连功能