惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
H
Help Net Security
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
A
About on SuperTechFans
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
宝玉的分享
宝玉的分享
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
博客园 - 【当耐特】
Microsoft Security Blog
Microsoft Security Blog
Martin Fowler
Martin Fowler
I
InfoQ
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
U
Unit 42
The Cloudflare Blog
Y
Y Combinator Blog

博客园 - Kriss Liu

绕开 CoreLab.MySql 验证 如何通过活动目录(ADSI)修改IIS6中的 Web 服务扩展 关于如何让页面同时下载多个文件的尝试 - Kriss Liu - 博客园 Remoting中的线程与网络通信内幕初探 Remoting多个信道(Chennel)的注册问题 基于强名称签名的代码访问保护及其改进 利用命名管道(Named Pipe)向Flash Player模拟Flash媒体文件 多样、互动的WinForm UI设计与开发思路(Flash、Html等) WinForm中设计时与DesignMode的区别 ServU插件设计 如何在C#中直接操作C++结构体 .NET中Flags枚举的使用 最近的计划。。VOS和DataSetBrowser。。 看完《仙剑奇侠传》前20集的一点牢骚。。 一套可嵌入或独立使用的翻页控件: WebPager(附源码) ASP.NET 2.0 中创建DataList子类控件时的一个错误 在.NET中使用DirectMusic 我与电脑的十年 关于 Windows Media DRM 0xC00D2840 错误
XMLDOM/XMLHTTP的跨域访问和页面代理
Kriss Liu · 2006-03-03 · via 博客园 - Kriss Liu

我们知道通过在页面中使用JavaScript调用微软的XMLDOM/XMLHTTP组件,可以方便的在页面中各种远程的资源,如网页和XML数据。不过由于IE默认安全级别的限制,我们并不能通过XMLDOM/XMLHTTP访问不同域的数据源。也就是说,www.siteA.com不能访问www.siteB.com里的页面。我们可以通过一个简单的本地页面代理来帮我们实现这个功能。新的流程将是: siteA.com/local.aspx (JavaScript)  -> siteA.com/agent.aspx?url=siteB.com/data.xml  -> siteB.com/data.xml

代码很简单,支持POST方式,但是GET方式的时候,URL里的其他参数目前都没有处理。

<%@ Page Language="C#" ClassName="PageAgent" ValidateRequest="false" %>

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>

<script type="text/C#" runat="server">
    
protected override void OnLoad(EventArgs e)
    
{
        Response.Clear();

        
string url = Request.QueryString["url"];
        
if (null == url || url.Trim().Length == 0)
        
{
            Response.End();
            
return;
        }


        
try
        
{
            WebRequest request 
= WebRequest.Create(url);
            request.Method 
= Request.RequestType;
            request.ContentType 
= Request.ContentType;

            
int reads = 0;
            
byte[] buffer = new byte[512];
            
            
if (Request.RequestType.Equals("POST", StringComparison.InvariantCultureIgnoreCase))
            
{
                
if (null != Request.Form) {
                    
using (Stream s = request.GetRequestStream())
                    
{
                        
while ((reads = Request.InputStream.Read(buffer, 0, buffer.Length)) > 0)
                        
{
                            s.Write(buffer, 
0, reads);
                        }

                        s.Flush();
                    }

                }

            }


            WebResponse response 
= request.GetResponse();
            Response.ContentType 
= response.ContentType;
            
using (Stream s = response.GetResponseStream())
            
{
                
while ((reads = s.Read(buffer, 0, buffer.Length)) > 0)
                
{
                    Response.OutputStream.Write(buffer, 
0, reads);
                }

            }


            response.Close();
        }

        
catch
        
{
        }


        Response.End();
    }

</script>