網絡實現工具類

網絡實現工具類網絡

using System;
using System.Text;
using System.Net;
using System.IO;
using Evt.Framework.Common;    
    
    /// <summary>
    /// 網絡實現工具類
    /// </summary>
    public class NetUtil
    {
        /// <summary>
        /// 經過GET請求得到響應文本
        /// </summary>
        /// <param name="url">將要被請求的URL</param>
        /// <param name="param">請求參數,各參數key=value之間使用參數鏈接符進行鏈接</param>
        /// <returns>響應文本</returns>
        public static string ResponseByGet(string url, string param)
        {
            if (String.IsNullOrEmpty(url))
                throw new MessageException("url不能爲空值");

            url = url.Trim();
            if (!String.IsNullOrEmpty(param))
            {
                if (!url.EndsWith("?")) url += "?";
                url += param;
            }

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                Stream stream = response.GetResponseStream();
                StreamReader sr = new StreamReader(stream, Encoding.UTF8);
                string text = sr.ReadToEnd();

                sr.Close();
                return text;
            }
            catch (Exception ex)
            {
                throw new MessageException("請求網絡發生錯誤:" + ex.Message + ",請稍候再試");                
            }
        }

        /// <summary>
        /// 經過POST請求得到響應文本
        /// </summary>
        /// <param name="url">將要被請求的URL</param>
        /// <param name="param">請求參數,各參數key=value之間使用參數鏈接符進行鏈接</param>
        /// <returns>響應文本</returns>
        public static string ResponseByPost(string url, string param)
        {
            if (String.IsNullOrEmpty(url))
                throw new MessageException("url不能爲空值");

            url = url.Trim();

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";

                if (!String.IsNullOrEmpty(param))
                {
                    byte[] paramArray = Encoding.UTF8.GetBytes(param);
                    request.ContentLength = paramArray.Length;

                    Stream stream = request.GetRequestStream();
                    stream.Write(paramArray, 0, paramArray.Length);
                    stream.Close();
                }

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
                string text = sr.ReadToEnd();

                sr.Close();
                return text;
            }
            catch (Exception ex)
            {
                throw new MessageException("請求網絡發生錯誤:" + ex.Message + ",請稍候再試");
            }
        }
    }
相關文章
相關標籤/搜索