如何在c#中获取父URL引用者

时间:2017-06-12 12:49:59

标签: javascript c# outputstream referrer clientip

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

正文中的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(其他网站),然后点击链接并转到zzz.com/1.html(我的网站)。 zzz.com/1.html页面包含我之前提到过的脚本我想知道zzz.com/1.html引用来自xyz.com,但我的c#页面显示zzz.com作为推荐者有意义。我怎么能得到zzz.com/1.html推荐人?! 我希望解释已经足够了。

由于

1 个答案:

答案 0 :(得分:0)

尝试在查询字符串中传递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);
}