ajax跨域访问代理文件下载(asp、php、asp.net)_AJAX教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:Ajax教程之简单应用,检测用户名是否存在
客户端页面index.html onal.dtd html xmlns=1999/xhtml head titleajax测试用户名是否存在/title script language=

最近做东西遇到了ajax跨域(cross domain)访问的问题,最后采用了Application Proxies 方式解决,即在本域内放置一个代理文件(视本域支持的开发语言选定asp、asp.net或是其他),此代理文件将url参数(QueryString)发送到目标域对应页面获取html代码,然后输出。ajax直接访问这个代理文件以达到跨域的目的。
基于asp.net的跨域访问代理文件c#代码如下:
<%@ Page Language="C#" AutoEventWireup="true" ResponseEncoding="utf-8" %>
<%@ Import Namespace=System.Net %>
<%@ Import Namespace=System.IO %>

<script runat="server">
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

string sourceUrl = "http://devspy.net";
this.Page.Response.Write(TransferHtmlPage(string.Concat(sourceUrl, "?", this.Page.Request.QueryString)));
}

public string TransferHtmlPage(string url)
{
string result = string.Empty;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
}
catch(Exception ex)
{
return string.Format(@"<p style='color:red;text-align:center;'>服务器获取文件内容出错:{0}</p>", ex.Message);
}

if (!CheckVersionWaterMark(result))
return @"<p style='color:red;text-align:center;'>版本水印失效,请联系相关技术人员。</p>";

return result;
}

public bool CheckVersionWaterMark(string inputString)
{
return true;//不验证水印了
//string pattern = "WaterMark";
//return Regex.IsMatch(inputString, pattern, RegexOptions.IgnoreCase);
}

</script>

另外还有基于asp和php的实现,不再列出,感兴趣的可以下载包含这三个文件的压缩包:
http://www.cnblogs.com/Files/cncxz/ajaxProxy.rar

 

分享:浅谈我眼中的Ajax
AJAX在去年确实火爆了,作为Web2.0时代的核心技术,关注度盛况空前,至今仍余音未绝,我一直从事于WinForm、Windows Mobile应用开发,虽然对Web开发略知一二,但从未向学习WinForm开发一样系统的学过Web,最近闲暇之余从头学了学ASP.NET,到现在才真正理解清

来源:模板无忧//所属分类:AJAX教程/更新时间:2010-02-26
相关AJAX教程