Use JavaScript and C # To obtain a referer,

Source: Internet
Author: User

Use JavaScript and C # To obtain a referer,

1. JavaScript

Copy codeThe Code is as follows:
/**
* Obtain the Referer of an HTTP request
* @ Ishost: whether to return the Host (website Homepage Address) if the Boolean Referer type is null)
*/
Function get_http_referer (ishost ){
If (ishost = undefined) {ishost = true ;}
If (document. referrer ){
Return document. referrer;
} Else {
If (ishost ){
Return window. location. protocol + "//" + window. location. host;
} Else {
Return "";
}
}
}

2. C #

Copy codeThe Code is as follows:
/// <Summary>
/// Obtain the Referer of the HTTP request
/// </Summary>
/// <Param name = "ishost"> whether to return the Host (website Homepage Address) when the Referer is empty </param>
/// <Returns> string </returns>
Public string GetReferer (bool ishost)
{
If (Request. UrlReferrer! = Null)
{
Return Request. UrlReferrer. ToString ();
}
Else
{
If (ishost)
{
Return Request. Url. Scheme + ": //" + Request. Url. Authority;
}
Else
{
Return "";
}
}
}

3. Obtain the parts of the Request URL in C #
URL: http: // localhost: 1897/News/Press/Content. aspx/123? Id = 1 # toc

Copy codeThe Code is as follows:
Request. ApplicationPath/
Request. PhysicalPath D: \ Projects \ Solution \ web \ News \ Press \ Content. aspx
System. IO. Path. GetDirectoryName (Request. PhysicalPath) D: \ Projects \ Solution \ web \ News \ Press
Request. PhysicalApplicationPath D: \ Projects \ Solution \ web \
System. IO. Path. GetFileName (Request. PhysicalPath) Content. aspx
Request. CurrentExecutionFilePath/News/Press/Content. aspx
Request. FilePath/News/Press/Content. aspx
Request. Path/News/Press/Content. aspx/123
Request. RawUrl/News/Press/Content. aspixels/123? Id = 1
Request. Url. AbsolutePath/News/Press/Content. aspx/123
Request. Url. AbsoluteUri http: // localhost: 1897/News/Press/Content. aspx/123? Id = 1
Request. Url. Scheme http
Request. Url. Host localhost
Request. Url. Port 1897
Request. Url. Authority localhost: 1897
Request. Url. LocalPath/News/Press/Content. aspx/123
Request. PathInfo/123.
Request. Url. PathAndQuery/News/Press/Content. aspx/123? Id = 1
Request. Url. Query? Id = 1
Request. Url. Fragment
Request. Url. Segments/
News/
Press/
Content. aspx/
123

4. JavaScript window. location object
Http://www.bkjia.com/article/57407.htm

How do I get the page referer in javascript and C? If you have any questions, contact me.

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.