技术文章 > [原创]C#开发WAP之GOOGLEADSENSE移动广告代码获取源代码

[原创]C#开发WAP之GOOGLEADSENSE移动广告代码获取源代码

2018-06-23 01:19

文档管理软件,文档管理系统,知识管理系统,档案管理系统的技术资料:

什么是 AdSense 移动广告?
AdSense 移动广告使发布商可以利用有针对性的 Google 广告,通过自己的移动网页获取收益。与 AdSense 内容广告类似,使用 AdSense 移动广告时 Google 也会按您网站(在此情况下即为您的移动网站)的内容来提供广告。每当您移动网站的访问者点击广告时,您就会获得收益。

移动广告是如何投放的?
凭借与 AdSense 内容广告相同的定位技术,AdSense 移动广告广告可与您移动网页的内容准确匹配。我们的抓取工具会扫描网页以确定其内容,然后自动投放与您的访问者所看到的内容相关的广告。

为使 Google 正确地将广告投放到您的移动网站,我们的抓取工具需要访问您的移动网页。我们用于抓取您网站的手机用户代理会包含“Mediapartners-Google“字符串,因此请设置您的网络服务器,允许包含“Mediapartners-Google“的所有用户代理访问您的移动内容。

另外请注意,如果您限制自己的移动网页,使其仅允许来自特定 IP 段的访问,Google 将无法抓取您的网页以进行广告定位。

Google adSense 移动广告也是 WAP 网站收入来源之一。虽然不是很多,积累起来也不少呢。
可惜的是,在 adSense 移动广告的程序设置中,没有支持 C# 的接口啊。

这里提供原创的相关代码,或许您有用。

[CODE]
CSHARP
using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Web.UI.HtmlControls;

namespace K50018.Wap
{

public class Google_adSense_Wap
{

/// <summary>
/// 当前Request对象
/// </summary>
public HttpRequest Request
{
get { return HttpContext.Current.Request; }
}

/// <summary>
/// 当前Response对象
/// </summary>
public HttpResponse Response
{
get { return HttpContext.Current.Response; }
}

public HttpServerUtility Server
{
get { return HttpContext.Current.Server; }
}

/// <summary>
/// 解析颜色
/// </summary>
/// <param name=“value“></param>
/// <param name=“random“></param>
/// <returns></returns>
public string googleColor(string value, int random)
{
string[] colorArray = value.Split(“,“);
return colorArray[random % (colorArray.Length + 1)];
}

/// <summary>
/// 读取分辨率
/// </summary>
/// <returns></returns>
public string googleScreenRes()
{
string screenRes = ““;
char delimiter = “x“;
if (Request.ServerVariables[“HTTP_UA_PIXELS“] != null)
{
screenRes = Request.ServerVariables[“HTTP_UA_PIXELS“].ToString();
}
if (screenRes == ““)
{
if (Request.ServerVariables[“HTTP_X_UP_DEVCAP_SCREENPIXELS“] != null)
{
screenRes = Request.ServerVariables[“HTTP_X_UP_DEVCAP_SCREENPIXELS“].ToString();
delimiter = “,“;
}
}
if (screenRes == ““)
{
if (Request.ServerVariables[“HTTP_X_JPHONE_DISPLAY“] != null)
{
screenRes = Request.ServerVariables[“HTTP_X_JPHONE_DISPLAY“].ToString();
delimiter = “*“;
}
}
if (screenRes != ““)
{
string[] resArray = screenRes.Split(delimiter);
if (resArray.Length == 2)
{
return “&u_w=“ + resArray[0] + “&u_h=“ + resArray[1];
}
}
return “&u_w=320&u_h=50“;
}

/// <summary>
/// 读取MUID信息
/// </summary>
/// <returns></returns>
public string googleMuid()
{
string rsl = ““;
string muid;
if (Request.ServerVariables[“HTTP_X_DCMGUID“] != null)
{
muid = Request.ServerVariables[“HTTP_X_DCMGUID“].ToString();
if (muid != ““)
{
rsl = “&muid=“ + muid;
}
}
if (Request.ServerVariables[“HTTP_X_UP_SUBNO“] != null)
{
muid = Request.ServerVariables[“HTTP_X_UP_SUBNO“].ToString();
if (muid != ““)
{
rsl = “&muid=“ + muid;
}
}
if (Request.ServerVariables[“HTTP_X_JPHONE_UID“] != null)
{
muid = Request.ServerVariables[“HTTP_X_JPHONE_UID“].ToString();
if (muid != ““)
{
rsl = “&muid=“ + muid;
}
}
if (Request.ServerVariables[“HTTP_X_EM_UID“] != null)
{
muid = Request.ServerVariables[“HTTP_X_EM_UID“].ToString();
if (muid != ““)
{
rsl = “&muid=“ + muid;
}
}
return rsl;
}

/// <summary>
/// 获取VIA变量
/// </summary>
/// <param name=“googleUserAgent“></param>
/// <returns></returns>
public string googleViaAndAccept(string googleUserAgent)
{
string rsl = ““;
if (googleUserAgent.Length == 0)
{
string via;
if (Request.ServerVariables[“HTTP_VIA“] != null)
{
via = Server.UrlEncode(Request.ServerVariables[“HTTP_VIA“].ToString());
if (via != ““)
{
rsl = “&via=“ + via;
}
}
string accept;
if (Request.ServerVariables[“HTTP_ACCEPT“] != null)
{
accept = Server.UrlEncode(Request.ServerVariables[“HTTP_ACCEPT“].ToString());
if (accept != ““)
{
//rsl += “&accept=“ + accept;
rsl += “&accept=text“;
}
}
}
return rsl;
}

/// <summary>
/// 计算事件差的函数DateDiff
/// </summary>
/// <param name=“IntervalName“></param>
/// <param name=“StartDate“></param>
/// <param name=“EndDate“></param>
/// <returns></returns>
public static int DateDiff(String IntervalName, DateTime dStart, DateTime dEnd)
{
TimeSpan TS = new TimeSpan(dEnd.Ticks - dStart.Ticks);
if (IntervalName == “s“) return (int)TS.TotalSeconds;
if (IntervalName == “m“) return (int)TS.TotalMinutes;
if (IntervalName == “h“) return (int)TS.TotalHours;
if (IntervalName == “d“) return (int)TS.TotalDays;
if (IntervalName == “w“) return (int)TS.TotalDays / 7;
if (IntervalName == “M“) return (int)TS.TotalDays / 30;
if (IntervalName == “y“) return (int)TS.TotalDays / 365;
return 0;
}

/// <summary>
/// 获取Google移动广告信息(AJAX)
/// </summary>
public string Execute()
{
int googleTime = DateDiff(“s“, DateTime.Parse(“1970-01-01 00:00:00“), System.DateTime.Now);
int googleDt = (1000 * googleTime); // + Math.Round(1000 * (Timer - Int(Timer)));
string googleScheme = “http://“;
string googleUserAgent = ““;
if(Request.ServerVariables[“HTTP_USER_AGENT“] != null)
{
googleUserAgent = Server.UrlEncode(Request.ServerVariables[“HTTP_USER_AGENT“].ToString());
}
if (Request.ServerVariables[“HTTPS“] != null)
{
if (Request.ServerVariables[“HTTPS“].ToString() == “on“)
{
googleScheme = “https://“;
}
}

StringBuilder sb = new StringBuilder();
sb.Append(“http://pagead2.googlesyndication.com/pagead/ads?“);
sb.Append(“ad_type=text_image“);
sb.Append(“&channel=2781727753“);
sb.Append(“&client=ca-mb-pub-0969306485724541“);
sb.Append(“&dt=“ + googleDt);
sb.Append(“&format=mobile_single“);
sb.Append(“&ip=“ + Server.UrlEncode(Request.ServerVariables[“REMOTE_ADDR“].ToString()));
sb.Append(“&markup=wml“);
sb.Append(“&oe=gb2312“);
//sb.Append(“&output=xhtml“);
sb.Append(“&output=wml“);
if (Request.ServerVariables[“HTTP_REFERER“] != null)
{
sb.Append(“&ref=“ + Server.UrlEncode(Request.ServerVariables[“HTTP_REFERER“].ToString()));
}
else
{
sb.Append(“&ref=“);
}
sb.Append(“&url=“ + Server.UrlEncode(googleScheme + Request.ServerVariables[“HTTP_HOST“].ToString() + Request.ServerVariables[“URL“].ToString()));
sb.Append(“&useragent=“ + googleUserAgent);
sb.Append(googleScreenRes());
sb.Append(googleMuid());
sb.Append(googleViaAndAccept(googleUserAgent));

string googleAdUrl = sb.ToString();

MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
_xmlhttp.open(“GET“, googleAdUrl, false, null, null);
_xmlhttp.send(““);
if (_xmlhttp.readyState == 4)
{
return _xmlhttp.responseText;
}
return ““;

}
}
}
[/CODE]

北京联高软件开发有限公司
因工作而快乐!