C#分析URL参数获取参数和值得对应列表(二)

不错博客:

[C#HttpHelper]官方产品发布与源码下载---苏飞版
http://www.sufeinet.com/thread-3-1-1.html

http://blog.csdn.net/xxj_jing/article/details/8107882

http://zhoufoxcn.blog.51cto.com/792419/561934/

http://www.dedecms.com/knowledge/program/asp-donet/2012/0803/4214.html

http://blog.csdn.net/zhoufoxcn/article/details/6404236

C# winform post 请求指定 url

/// <summary>
    /// 请求信息帮助
    /// </summary>
    public class HttpHelper
    {
        /// <summary>
        /// post 请求指定地址返回响应数据
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="postData">请求数据</param>
        /// <param name="timeout">请求超时时间(毫秒)</param>
        /// <param name="requestCoding">请求编码</param>
        /// <param name="responseCoding">响应编码</param>
        /// <returns></returns>
        public string PostRequest(string url, Dictionary<string, string> postData, int timeout, Encoding requestCoding, Encoding responseCoding)
        {
            string postStr = string.Empty;
            if (postData != null)
            {
                postData.All(o =>
                               {
                                   if (string.IsNullOrEmpty(postStr))
                                       postStr = string.Format("{0}={1}", o.Key, o.Value);
                                   else
                                       postStr += string.Format("&{0}={1}", o.Key, o.Value);

                                   return true;
                               });
            }
            return PostRequest(url, postStr, timeout, requestCoding, responseCoding);
        }
        /// <summary>
        /// post 请求指定地址返回响应数据
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="postData">请求数据</param>
        /// <param name="timeout">请求超时时间(毫秒)</param>
        /// <param name="requestCoding">请求编码</param>
        /// <param name="responseCoding">响应编码</param>
        /// <returns></returns>
        public string PostRequest(string url, string postData, int timeout, Encoding requestCoding, Encoding responseCoding)
        {
            string postUrl = VerifyUrl(url);
            try
            {
                //写入POSTDATE里
                byte[] byteArray = requestCoding.GetBytes(postData);
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                webRequest.Method = "POST";
                webRequest.Timeout = timeout;
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.ContentLength = byteArray.Length;
                Stream newStream = webRequest.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);
                newStream.Close();
                //接收返回信息:
                HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
                StreamReader php = new StreamReader(response.GetResponseStream(), responseCoding);
                string result = php.ReadToEnd();
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 验证URL
        /// </summary>
        /// <param name="url">待验证 URL</param>
        /// <returns></returns>
        private string VerifyUrl(string url)
        {
            if (string.IsNullOrEmpty(url))
                throw new Exception("URL 地址不可以为空!");

            if (url.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase))
                return url;

            return string.Format("http://{1}", url);
        }
    }

 

 /// <summary>
    /// Http请求
    /// </summary>
    public class HttpUtility
    {
        /// <summary>
        ///通用HttpWebRequest
        /// </summary>
        /// <param name="method">请求方式(POST/GET)</param>
        /// <param name="url">请求地址</param>
        /// <param name="param">请求参数</param>
        /// <param name="onComplete">完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)</param>
        /// <returns>请求返回的结果</returns>
        public static string Request(string method, string url, string param, Action<HttpStatusCode, string> onComplete = null)
        {
            if (string.IsNullOrEmpty(url))
                throw new ArgumentNullException("URL");

            switch (method.ToUpper())
            {
                case "POST":
                    return Post(url, param, onComplete);
                case "GET":
                    return Get(url, param, onComplete);
                default:
                    throw new EntryPointNotFoundException("method:" + method);
            }
        }

        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="param">参数</param>
        /// <param name="onComplete">完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)</param>
        /// <returns>请求返回的结果</returns>
        public static string Post(string url, string param, Action<HttpStatusCode, string> onComplete = null)
        {
            UrlCheck(ref url);

            byte[] bufferBytes = Encoding.UTF8.GetBytes(param);

            var request = WebRequest.Create(url) as HttpWebRequest;//HttpWebRequest方法继承自WebRequest, Create方法在WebRequest中定义,因此此处要显示的转换
            request.Method = "POST";
            request.ContentLength = bufferBytes.Length;
            request.ContentType = "application/x-www-form-urlencoded";

            try
            {
                using (var requestStream = request.GetRequestStream())
                {
                    requestStream.Write(bufferBytes, 0, bufferBytes.Length);
                }
            }
            catch (WebException ex)
            {

                return ex.Message;
            }

            return HttpRequest(request, onComplete);
        }

        /// <summary>
        /// GET请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="param">参数</param>
        /// <param name="onComplete">完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)</param>
        /// <returns>请求返回结果</returns>
        public static string Get(string url, string param, Action<HttpStatusCode, string> onComplete = null)
        {
            UrlCheck(ref url);

            if (!string.IsNullOrEmpty(param))
                if (!param.StartsWith("?"))
                    param += "?" + param;
                else
                    param += param;

            var request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";

            return HttpRequest(request, onComplete);
        }

        /// <summary>
        /// 请求的主体部分(由此完成请求并返回请求结果)
        /// </summary>
        /// <param name="request">请求的对象</param>
        /// <param name="onComplete">完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)</param>
        /// <returns>请求返回结果</returns>
        private static string HttpRequest(HttpWebRequest request, Action<HttpStatusCode, string> onComplete = null)
        {
            HttpWebResponse response = null;

            try
            {
                response = request.GetResponse() as HttpWebResponse;
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
            }

            if (response == null)
            {
                if (onComplete != null)
                    onComplete(HttpStatusCode.NotFound, "请求远程返回为空");
                return null;
            }

            string result = string.Empty;
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }

            if (onComplete != null)
                onComplete(response.StatusCode, result);

            return result;

        }

        /// <summary>
        /// URL拼写完整性检查
        /// </summary>
        /// <param name="url">待检查的URL</param>
        private static void UrlCheck(ref string url)
        {
            if (!url.StartsWith("http://") && !url.StartsWith("https://"))
                url = "http://" + url;
        }
    }

View Code

 

  

时间: 2024-10-10 20:51:34

C#分析URL参数获取参数和值得对应列表(二)的相关文章

C#分析URL参数获取参数和值得对应列表

原文: C#分析URL参数获取参数和值得对应列表 /// <summary> /// 分析url链接,返回参数集合 /// </summary> /// <param name="url">url链接</param> /// <param name="baseUrl"></param> /// <returns></returns> public static Syste

C#分析URL参数获取参数和值得对应列表(一)

C#操作Url参数 http://www.cnblogs.com/RobotH/archive/2008/11/17/1335322.html 用 C# 分析 URL 中的参数信息 http://www.cnblogs.com/rchen/archive/2005/06/23/179627.html C#获取URL参数值  http://www.cnblogs.com/gaizai/archive/2010/05/27/1743485.html C#分析URL参数获取参数和值得对应列表 http

JS控制静态页面之间传递参数获取参数并应用的简单实例_javascript技巧

在项目中遇到这也一个问题: 有a.html和b.html. 1.a页面已经打开,b页面尚未打开,我希望在a页面设置好一些列参数,比如背景色,宽度等参数,传递给b页面,好让b页面在打开就能应用. 2.a页面已经打开,b页面无论是否打开.在a页面需要获取到b页面的一些元素甚至变量,以便于应用到a页面. 注意:不涉及跨域问题. 想了很久,终于想到了解决方案. 第一个问题,我们可以利用html页面锚点的特性,将参数通过url传递给b页面 这是a页面代码: <button>跳转设置</button

JS获取URL中的参数数据

 这篇文章主要介绍了JS获取URL中的参数数据,有需要的朋友可以参考一下   代码如下: function getParam(paramName) {     paramValue = "";     isFound = false;     if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {      

PHP URL参数获取方式的四种例子

 这篇文章主要介绍了PHP URL参数获取方式的四种例子,php url参数解析的4种方法,需要的朋友可以参考下 在已知URL参数的情况下,我们可以根据自身情况采用$_GET来获取相应的参数信息($_GET['name']);那,在未知情况下如何获取到URL上的参数信息呢?   第一种.利用$_SERVER内置数组变量   相对较为原始的$_SERVER['QUERY_STRING']来获取,URL的参数,通常使用这个变量返回的会是类似这样的数据:name=tank&sex=1 如果需要包含文件

javascript-js获取url中的参数乱码

问题描述 js获取url中的参数乱码 我的url为:test.jsp?lastname=杨涛 js是这么写的 window.onload=function(){ alert(GetQueryString("lastname")); } function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i&q

方法-java获取URL是否带参数过来的

问题描述 java获取URL是否带参数过来的 URL输入的是xxxx/doLogin.action 或者是xxxx/doLogin.action?locale=zh_CN 用下面的方法来判断,出现空指针问题,求指教 if(!(httpServletRequest.getQueryString()==null)){ String url = httpServletRequest.getQueryString(); boolean flag=url.matches("locale=[a-z]{1,2

url中的=被转义为%3D,request.getparameter()获取参数失败,

问题描述 url中的=被转义为%3D,request.getparameter()获取参数失败, 最近在做微信开放平台,发现微信回调之后会把原来的url中的"="转义为%3D ,导致在request.getparameter()中获取的时候,获取不到参数的值,获取到的为null,怎么问题怎么解决呢? 解决方案 先用urldecode解码,然后再用split切分,获取.

php解析url并得到url中的参数及获取url参数的四种方式_php实例

下面一段代码是php解析url并得到url中的参数,代码如下所示: <?php $url = 'http://www.baidu.com/index.php?m=content&c=index&a=lists&catid=6&area=0&author=0&h=0&region=0&s=1&page=1'; $arr = parse_url($url); var_dump($arr); $arr_query = convertU