C#中常用的WebRequest方法

来源:互联网 发布:阿里云美国服务器 vpn 编辑:程序博客网 时间:2024/06/06 04:33

建立一个静态类,经这些方法都写在静态类中,以便在项目中可以随意调用

public static class WS

 {

  //取得Request的int型值,From优先于QueryString被取出,如果获取不到,返回int.MinValue

  public static int RequestInt(string parName)

     {

          return RequestInt(parName,int.MinValue); 

     }

  //取得Request的int型值,Form优先于QueryString被取出,如果获取不到返回默认值

   public static int RequestInt(string parName,int defaultValue)

    {

        try{

                return int.Parse(RequestString(parName));

            }

        catch{

                   return defaultValue;

                }

    }

//取得Request的string型值,From优先于QueryString被取出,任何错误返回“”

 public static string RequestString(object key)

    {

        return RequeatString(key,"");

    }

 public static string RequestString(object key,string defaultValue)

   {

       if(HttpContext.Current.Request.Form[key.ToString()]!=null)

          {

               return System.Web.HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Form[key.ToString()].ToString()).ToString();

          }

        if(HttpContext.Current.Request.QueryString[key.ToString()]!=null)

          {

              return System.Web.HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.QueryString[key.ToString()].ToString());

          }

          return defaultValue;

   }

//获取当前完整url

  public static string GetUrl()

     {

          return HttpContext.Current.Request.Url.ToString();

     }

 }


0 0
原创粉丝点击