ASP.NET写的AJAX跨域代理(收藏)
来源:互联网 发布:部落冲突 知乎 编辑:程序博客网 时间:2024/05/01 16:11
我用AJAX写了个ArcIMS的WebGIS,在做测试的时候链接的是网络上别的城市的数据,当然,不可避免就出现了跨域问题咯。AJAX跨域的本质是JS的问题,JS写的程序只允许访问本域内的数据,而跨域则受到限制,在IE中会弹出一个警告,在FF中直接就被终止了,所以我的这个WebGIS在解决跨域之前无法在FF中使用(如果数据在本域还是可以的)。
好了,跨域解决的方法很简单,即在本域中新建一个不使用XHR对象发送请求并接收响应的程序即可,因此,asp、aspnet、jsp或servlet都能够担当此重任。下面是我写的一个aspnet代理:
- using System;
- using System.Data;
- using System.Text;
- using System.IO;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Net;
- public partial class Proxy : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- string url = Request["URL"].ToString();
- string clientversion = Request["ClientVersion"].ToString();
- string customservice = string.Empty;
- string URL = string.Empty;
- if (Request["CustomService"] != null)//注意不能要form的参数
- {
- customservice = Request["CustomService"].ToString();
- URL = url + "&CustomService=" + customservice+ "&ClientVersion=" + clientversion ;
- }
- else
- {
- URL = url + "&ClientVersion=" + clientversion;
- }
- string arcxml = Request["ArcXML"].ToString();
- byte[] postBytes = Encoding.UTF8.GetBytes(arcxml);
- string result = string.Empty;
- HttpWebRequest HttpWReq =(HttpWebRequest)WebRequest.Create(URL);
- HttpWReq.Method = "Post";
- HttpWReq.ContentType = "application/x-www-form-urlencoded";
- HttpWReq.ContentLength = postBytes.Length;
- using (Stream reqStream = HttpWReq.GetRequestStream())
- {
- reqStream.Write(postBytes, 0, postBytes.Length);
- }
- HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
- StreamReader reader = new StreamReader(HttpWResp.GetResponseStream(), Encoding.UTF8);
- result = reader.ReadToEnd();
- //HttpWResp.Close();
- Response.Write(result);
- Response.End();
- }
- }
- ASP.NET写的AJAX跨域代理(收藏)
- ASP.NET写的AJAX跨域代理
- 基于Asp.net的ajax 跨域访问代理函数
- asp.net ajax跨二级域代理页面
- ajax跨域访问代理文件下载(asp、php、asp.net)
- 看这个帖子写的不错,就转来收藏了..(转asp.net为静态页)
- 【收藏】利用AJAX和ASP.NET实现简单聊天室
- AjaxControlToolkit requires ASP.NET Ajax 4.0 scripts错误 收藏
- ASP.NET的网上资料收藏
- 收藏的一些ASP.net 2.0资料
- 收藏的一些ASP.net 2.0资料
- 收藏的一些ASP.net 2.0资料
- 收藏的一些ASP.net 2.0资料
- 整理的asp.net资料(收藏)
- 整理的asp.net资料(收藏)
- 整理的asp.net资料(收藏)
- 整理的asp.net资料(收藏)
- asp.net ajax的应用
- SQL Server不存在或访问被拒绝
- Web.config详解 + asp.net优化方法
- GIS地理信息软件
- 虛函数,抽象方法,接口之间的区别
- CVS心得点滴--关于tag和branch (一)
- ASP.NET写的AJAX跨域代理(收藏)
- log4j简介
- 开篇
- Java字节码揭秘——第四部分
- VS2005的默认路径
- 无敌命令
- 回答Visual Studio 2005 无法更新Access的问题
- 美国强悍女子近距离抓拍撼人巨型龙卷风(图)
- C#操作Excel(导入导出)