网络实现工具类

网络实现工具类

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 + ",请稍候再试");
            }
        }
    }

  

posted @ 2014-01-03 10:47  会飞的剑  阅读(315)  评论(0编辑  收藏  举报