C# 中请求数据方式

#region 根据URL获取结果集
        /// <summary>
        /// 根据URL获取结果集 默认为GET,如果数据量大了可以传入POST
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <param name="type">默认为GET,删除时用DELETE</param>
        /// <returns>String类型的,Json格式的结果集</returns>
        public static string GetUrlResult(string url, string type = "GET")
        {
            string result = string.Empty;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = type;
            request.ContentType = "application/x-www-form-urlencoded";
            Stream resStream = null;
            StreamReader sr = null;
            try
            {
                WebResponse response = request.GetResponse();
                resStream = response.GetResponseStream();
                sr = new StreamReader(resStream, System.Text.Encoding.UTF8);
                result = sr.ReadToEnd();
                resStream.Close();
                sr.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return result;
        }
        #endregion

#region 根据URL获取结果集
        /// <summary>
        /// 根据URL获取结果集 默认为GET,如果数据量大了可以传入POST
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <param name="proxy">代理IP</param>
        /// <param name="type">默认为GET,删除时用DELETE</param>
        /// <returns>String类型的,Json格式的结果集</returns>
        public static string GetUrlResult(string url, WebProxy proxy, string type = "GET")
        {
            string result = string.Empty;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Proxy = proxy;
            request.Method = type;
            request.Timeout = 1000 * 10;
            request.ContentType = "application/x-www-form-urlencoded";
            Stream resStream = null;
            StreamReader sr = null;
            try
            {
                WebResponse response = request.GetResponse();
                resStream = response.GetResponseStream();
                sr = new StreamReader(resStream, System.Text.Encoding.UTF8);
                result = sr.ReadToEnd();
                resStream.Close();
                sr.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return result;
        }
        #endregion

#region 通过POST方式去TRS创建豆腐块
        /// <summary>
        /// 通过POST方式去TRS创建豆腐块
        /// </summary>
        /// <param name="url">url</param>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public static string PostUrlResult(string url, string param)
        {
            string result = string.Empty;
            byte[] byteArray = Encoding.UTF8.GetBytes(param);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";

Stream newStream = null;
            StreamReader sr = null;
            try
            {
                newStream = request.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);
                newStream.Close();
                //获取响应
                WebResponse response = request.GetResponse();
                sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                result = sr.ReadToEnd();
                sr.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return result;
        }
        #endregion

#region 根据IP和端口号检查代理IP是否有效
        /// <summary>
        /// 根据IP和端口号检查代理IP是否有效
        /// </summary>
        /// <param name="ip">代理IP</param>
        /// <param name="port">端口号</param>
        /// <returns></returns>
        public static bool AgentIsAvailable(string ip, int port)
        {
            bool result = false;
            HttpWebRequest webRequest;
            HttpWebResponse webResponse;
            StreamReader sr = null;
            try
            {
                WebProxy proxy = new WebProxy(ip, port);
                webRequest = WebRequest.Create(ConfigurationManager.AppSettings["SSWBValidIPUrl"]) as HttpWebRequest;
                webRequest.Proxy = proxy;
                webRequest.Timeout = 1000 * 5;
                webResponse = webRequest.GetResponse() as HttpWebResponse;
                sr = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
                string str = sr.ReadToEnd();
                if (str.Contains(ConfigurationManager.AppSettings["SSWBContainsString"]))
                    result = true;
                sr.Close();
                sr.Dispose();
            }
            catch (Exception ex)
            {
                Logging.Error(string.Format("检验代理IP是否有效时异常,异常信息:{0}", ex.Message));
            }
            return result;
        }
        #endregion

上一篇:jquery属性的相关js实现方法


下一篇:NYOJ128 前缀式计算(栈的运用)