ASP. NET Request related operations

Source: Internet
Author: User
Tags servervariables

Using system;
Using system. Web;
Using system. text;

Namespace pub. Class
{
/// <Summary>
/// Requests operation class
/// </Summary>
Public class request2 {
# Region get/getint/getfloat
/// <Summary>
/// Receive data transfer
/// </Summary>
/// <Param name = "varname"> parameter name </param>
/// <Returns> value corresponding to the parameter </returns>
Static Public String get (string varname)
{
String varvalue = "";
If (httpcontext. Current. request [varname]! = NULL)
Varvalue = httpcontext. Current. request [varname]. tostring ();
Return varvalue;
}
/// <Summary>
/// Getint
/// </Summary>
/// <Param name = "varname"> </param>
/// <Param name = "defvalue"> </param>
/// <Returns> </returns>
Static public int getint (string varname, int defvalue) {return get (varname). toint (defvalue );}
/// <Summary>
/// Getfloat
/// </Summary>
/// <Param name = "varname"> </param>
/// <Param name = "defvalue"> </param>
/// <Returns> </returns>
Static public float getfloat (string varname, int defvalue) {return get (varname). tofloat (defvalue );}
# Endregion

# Region getq/getqint/getqfloat
/// <Summary>
/// Obtain URL parameters
/// </Summary>
/// <Param name = "varname"> parameter name </param>
/// <Returns> return parameters </returns>
Static Public String getq (string varname)
{
String varvalue = "";
If (httpcontext. Current. Request. querystring [varname]! = NULL)
Varvalue = httpcontext. Current. Request. querystring [varname]. tostring ();
Return varvalue;
}
/// <Summary>
/// Getqint
/// </Summary>
/// <Param name = "varname"> </param>
/// <Param name = "defvalue"> </param>
/// <Returns> </returns>
Static public int getqint (string varname, int defvalue) {return getq (varname). toint (defvalue );}
/// <Summary>
/// Getqfloat
/// </Summary>
/// <Param name = "varname"> </param>
/// <Param name = "defvalue"> </param>
/// <Returns> </returns>
Static public float getqfloat (string varname, int defvalue) {return getq (varname). tofloat (defvalue );}
# Endregion

# Region getf/getfint/getffloat
/// <Summary>
/// Retrieve the data submitted by post
/// </Summary>
/// <Param name = "varname"> name </param>
/// <Returns> return value </returns>
Static Public String getf (string varname)
{
String varvalue = "";
If (httpcontext. Current. Request. Form [varname]! = NULL)
Varvalue = httpcontext. Current. Request. Form [varname]. tostring ();
Return varvalue;
}
/// <Summary>
/// Getfint
/// </Summary>
/// <Param name = "varname"> </param>
/// <Param name = "defvalue"> </param>
/// <Returns> </returns>
Static public int getfint (string varname, int defvalue) {return getf (varname). toint (defvalue );}
/// <Summary>
/// Getffloat
/// </Summary>
/// <Param name = "varname"> </param>
/// <Param name = "defvalue"> </param>
/// <Returns> </returns>
Static public float getffloat (string varname, int defvalue) {return getf (varname). tofloat (defvalue );}
# Endregion

# Region ispost/isget
/// <Summary>
/// Determine whether the current page has received the POST request
/// </Summary>
/// <Returns> whether the POST request is received </returns>
Public static bool ispost ()
{
Return httpcontext. Current. Request. httpmethod. Equals ("Post ");
}
/// <Summary>
/// Determine whether the current page has received the GET request
/// </Summary>
/// <Returns> whether the GET request is received </returns>
Public static bool isget ()
{
Return httpcontext. Current. Request. httpmethod. Equals ("get ");
}
# Endregion

# Region server variable name
/// <Summary>
/// Return the specified server variable information
///
/// </Summary>
/// <Param name = "strname"> server variable name </param>
/// <Returns> server variable information </returns>
Public static string getserverstring (string strname)
{
If (httpcontext. Current. Request. servervariables [strname] = NULL)
Return "";
Return httpcontext. Current. Request. servervariables [strname]. tostring ();
}
# Endregion

# Region getrawurl/isbrowserget/issearchenginesget/getpagename/getqparamcount/getfparamcount/getparamcount/
/// <Summary>
/// Obtain the original URL of the current request (the part after the domain information in the URL, including the query string (if any ))
/// </Summary>
/// <Returns> original URL </returns>
Public static string getrawurl ()
{
Return httpcontext. Current. Request. rawurl;
}
/// <Summary>
/// Determine whether the current access is from the browser software
/// </Summary>
/// <Returns> whether the current access is from the browser software </returns>
Public static bool isbrowserget ()
{
String [] browsername = {"ie", "Opera", "Netscape", "Mozilla", "Konqueror", "Firefox "};
String curbrowser = httpcontext. Current. Request. browser. type. tolower ();
For (INT I = 0; I <browsername. length; I ++ ){
If (curbrowser. indexof (browsername [I])> = 0) return true;
}
Return false;
}
/// <Summary>
/// Determine whether the search engine is connected
/// </Summary>
/// <Returns> whether it is from the search engine link </returns>
Public static bool issearchenginesget (){
If (httpcontext. Current. Request. urlreferrer! = NULL ){
String [] strarray = new string [] {"google", "yahoo", "MSN", "Baidu", "sogou", "Sohu", "Sina ", "163", "Lycos", "Tom", "yisou", "iask", "Soso", "gougou", "zhongsou "};
String STR = httpcontext. Current. Request. urlreferrer. tostring (). tolower ();
For (INT I = 0; I <strarray. length; I ++ ){
If (Str. indexof (strarray [I])> = 0) return true;
}
}
Return false;
}
/// <Summary>
/// Obtain the name of the current page
/// </Summary>
/// <Returns> name of the current page </returns>
Public static string getpagename ()
{
String [] urlarr = httpcontext. Current. Request. url. absolutepath. Split ('/');
Return urlarr [urlarr. Length-1]. tolower ();
}
/// <Summary>
/// The total number of returned form or URL parameters
/// </Summary>
/// <Returns> </returns>
Public static int getparamcount ()
{
Return httpcontext. Current. Request. Form. Count + httpcontext. Current. Request. querystring. count;
}
/// <Summary>
/// Get paramcount
/// </Summary>
/// <Returns> </returns>
Public static int getqparamcount () {return (httpcontext. Current. Request. querystring. Count );}
/// <Summary>
/// Post paramcount
/// </Summary>
/// <Returns> </returns>
Public static int getfparamcount () {return (httpcontext. Current. Request. Form. Count );}
# Endregion

# Region getcurrentfullhost/gethost/getip/geturl/getreferrer/saverequestfile/getos/getbrowser
/// <Summary>
/// Obtain the full IP including the port
/// </Summary>
/// <Returns> IP address and port </returns>
Public static string getcurrentfullhost ()
{
Httprequest request = httpcontext. Current. request;
If (! Request. url. isdefaultport)
Return string. Format ("{0 }:{ 1}", request. url. Host, request. url. Port. tostring ());
Return request. url. Host;
}
/// <Summary>
/// Obtain the Host Name
/// </Summary>
/// <Returns> </returns>
Public static string gethost () {return httpcontext. Current. Request. url. Host ;}
/// <Summary>
/// Foreground URL
/// </Summary>
/// <Returns> </returns>
Public static string geturl () {return httpcontext. Current. Request. url. tostring ();}
/// <Summary>
/// Source URL
/// </Summary>
/// <Returns> </returns>
Public static string getreferrer (){
String STR = NULL;
Try {
STR = getserverstring ("http_referer"). Trim ();
If (Str. Length = 0) STR = httpcontext. Current. Request. urlreferrer. tostring ();
} Catch {}

If (STR = NULL) Return "";
Return STR;
}
/// <Summary>
/// Save the request file
/// </Summary>
/// <Param name = "path"> Save to file name </param>
Public static void saverequestfile (string path)
{
If (httpcontext. Current. Request. Files. Count> 0) httpcontext. Current. Request. Files [0]. saveas (PATH );
}
# Region getip
/// <Summary>
/// Obtain the IP address
/// </Summary>
/// <Returns> returned IP address </returns>
Public static string getip (){
String result = string. empty;
Result = httpcontext. Current. Request. servervariables ["http_x_forwarded_for"];

If (result! = NULL & result! = String. Empty) {// a proxy may exist.
If (result. indexof (".") =-1) Result = NULL;
Else {
If (result. indexof (",")! =-1) {// There are ",". Multiple proxies are estimated. Obtain the first IP address that is not an intranet IP address.

Result = result. Replace ("", ""). Replace ("'","");
String [] temparyip = result. Split (",;". tochararray ());
For (INT I = 0; I <temparyip. length; I ++ ){
If (temparyip [I]. isip ()
& Temparyip [I]. substring (0, 3 )! = "10 ."
& Temparyip [I]. substring (0, 7 )! = "192.168"
& Temparyip [I]. substring (0, 7 )! = "172.16 .")
{
Return temparyip [I]; // locate an address that is not an intranet address
}
}
}
Else if (result. isip () // The proxy is in the IP Format.
Return result;
Else
Result = NULL; // the content in the proxy is not an IP address and the IP address is used.
}

}

String IPaddress = (httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = NULL
& Httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = String. Empty)
? Httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]
: Httpcontext. Current. Request. servervariables ["remote_addr"];

If (null = Result | result = string. Empty)
Result = httpcontext. Current. Request. servervariables ["remote_addr"];

If (result = NULL | result = string. Empty)
Result = httpcontext. Current. Request. userhostaddress;

Return result;
}
# Endregion
# Region getos
/// <Summary>
/// Retrieve the Operating System
/// </Summary>
/// <Returns> return to the Operating System </returns>
Public static string getos (){
Httpbrowsercapabilities BC = new httpbrowsercapabilities ();
BC = system. Web. httpcontext. Current. Request. browser;
Return BC. Platform;
}
# Endregion
# Region getbrowser
/// <Summary>
/// Reader
/// </Summary>
/// <Returns> return to the browser </returns>
Public static string getbrowser ()
{
Httpbrowsercapabilities BC = new httpbrowsercapabilities ();
BC = system. Web. httpcontext. Current. Request. browser;
Return BC. type;
}
# Endregion
# Endregion
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.