如何在 C# 中获取父 url 引荐来源网址

How to get parent url referrer in c#

我写了一个代码作为网络计数器。它在 OutPutStream 中生成 javascript 输出,因此我可以使用另一个网站中的脚本为其提供免费的网站计数器。 问题是,当我想获取引荐来源网址时,它 returns 放置脚本的页面不是真正的引荐来源网址。 这是我的代码:

html 正文中的代码:

<script type="text/javascript" language="javascript" src="counter.aspx?siteid=2"></script>

和 counter.aspx.cs :

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;


    public partial class counter : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            Response.ContentType = "text/javascript";
                UICulture = "en-US";
                Culture = "en-US";
                int siteid = int.Parse(Request.QueryString["siteid"].ToString());
                string pubIp = new System.Net.WebClient().DownloadString("https://api.ipify.org");
                var ipResponse = GetCountryByIP(pubIp);
                HttpBrowserCapabilities browse = Request.Browser;
                string platform = browse.Platform;
                string browsername = browse.Browser;
 Counter cnt = new Counter();
            var counter = cnt.GetCounter(siteid, DateTime.Now, pubIp, Request.UrlReferrer.ToString(), platform, browsername, "", ipResponse.Country);
                    string text = "document.write('<div id=\"ShortCounter\" style=\" margin: 0px auto;width: 100px;min-height: 100px;font-family: Tahoma;font-size: x-small;background-color:" + counter.BackColor + ";color:" + counter.color + ";border:" + counter.BorderSize + "px " + counter.BorderStyle + " " + counter.BorderColor + ";\">" +
            "<div style=\"padding: 8px;\">" +
                "You Are Visitor Number:<br />" +
                counter.CounterNumber.ToString("N0") +
                "<br />" +
                "Today" +
            "<br />" +
                counter.Today.ToString("N0") +
                "<br />" +
                "This Week" +
            "<br />" +
                counter.ThisWeek.ToString("N0") +
                "<br />" +
                "This Month" +
            "<br />" +
                counter.ThisMounth.ToString("N0") +
                "<br />" +
            "</div>" +
            "<div style=\"width: 100%; background-color: darkred; text-align: center; padding-top: 2px; padding-bottom: 2px;\">" +
                Request.UrlReferrer.ToString() +
            "</div>');";

                    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text);

                    Response.OutputStream.Write(bytes, 0, bytes.Length);
    }

问题是:假设我在 xyz.com(其他站点),然后单击 link 并转到 zzz.com/1.html (我的网站)。 zzz.com/1.html 页面包含我之前提到的脚本我想知道 zzz.com/1.html 引荐来源网址 xyz.com 但我的页面显示我 zzz.com 作为引荐来源网址,这是有道理的。我怎么能得到 zzz.com/1.html 推荐人?! 我希望解释已经足够了。

谢谢

尝试在查询字符串中传递 referer,如下所示

<script language="JavaScript" type="text/javascript">
var referer = document.referrer;
$.getScript("counter.aspx?siteid=2&referedby="+referer);
</script>

现在在代码中使用这个查询字符串

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class counter : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        Response.ContentType = "text/javascript";
            UICulture = "en-US";
            Culture = "en-US";
            int siteid = int.Parse(Request.QueryString["siteid"].ToString());
            string referedby = Request.QueryString["referedby"];
            string pubIp = new System.Net.WebClient().DownloadString("https://api.ipify.org");
            var ipResponse = GetCountryByIP(pubIp);
            HttpBrowserCapabilities browse = Request.Browser;
            string platform = browse.Platform;
            string browsername = browse.Browser;
 Counter cnt = new Counter();
        var counter = cnt.GetCounter(siteid, DateTime.Now, pubIp, referedby, platform, browsername, "", ipResponse.Country);
                string text = "document.write('<div id=\"ShortCounter\" style=\" margin: 0px auto;width: 100px;min-height: 100px;font-family: Tahoma;font-size: x-small;background-color:" + counter.BackColor + ";color:" + counter.color + ";border:" + counter.BorderSize + "px " + counter.BorderStyle + " " + counter.BorderColor + ";\">" +
        "<div style=\"padding: 8px;\">" +
            "You Are Visitor Number:<br />" +
            counter.CounterNumber.ToString("N0") +
            "<br />" +
            "Today" +
        "<br />" +
            counter.Today.ToString("N0") +
            "<br />" +
            "This Week" +
        "<br />" +
            counter.ThisWeek.ToString("N0") +
            "<br />" +
            "This Month" +
        "<br />" +
            counter.ThisMounth.ToString("N0") +
            "<br />" +
        "</div>" +
        "<div style=\"width: 100%; background-color: darkred; text-align: center; padding-top: 2px; padding-bottom: 2px;\">" +
            Request.UrlReferrer.ToString() +
        "</div>');";

                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text);

                Response.OutputStream.Write(bytes, 0, bytes.Length);
}