Cross Domain XmlHttpRequests (http://benreichelt.net/blog/2005/4/11/Cross-Domain-XmlHttpRequests-Aja...

Cross Domain XmlHttpRequests (Ajax)

One problem you run into when using client side xml calls is the
issue of getting some xml from a different domain.  Making a
cross domain request is simply denied in Firefox, and I believe it is
denied in SP2 IE, however pre-SP2 you were just alerted to the fact
that a cross domain request was being made.  On some of our pages
we want to include Yahoo news feeds that
relate to the content on the page.  Since we couldn�t just make
the request (Response.Redirects also do not work), I wrote a little
page that takes in another url as a querystring parameter, makes its
own web request to that page, and streams the content down to the
client.  This allows you to get any feed from any domain you want,
so it can come in pretty handy for the scenario I described
above.  Here is the code from the intermediate page: 

UPDATE:
Sorry for the poor formatting, I guess I’m not using CopySourceAsHTML
in the correct way. If you view the post on my blog page, the
formatting is intact.

   



    
1   Using directives #region Using directives

    
2 

    
3 using System;

    
4 using System.Text;

    
5 using System.Xml;

    
6 

    
7 #endregion


    
8  

    
9   namespace  MyApp.Web.Services

   
10   {

  
11     public class Redirecter : System.Web.UI.Page

   
12     {

  
13         protected override void OnInit(EventArgs e)

   
14         {

  
15             Load += new EventHandler(Page_Load);

  
16         }


   
17 

   
18         void Page_Load(object sender, EventArgs e)

   
19         {

   
20             XmlDocument responseDoc;

   
21             string url = Request.QueryString[“Url”];

   
22 

   
23             if (url != null && url.Length > 0)

   
24                 responseDoc = GetExternalFeed(url);

   
25             else

   
26                 responseDoc = GetError();

   
27 

   
28             Response.ContentType = “text/xml”;

   
29             Response.Write(responseDoc.InnerXml);

   
30             Response.End();

   
31         }


   
32 

   
33         private XmlDocument GetExternalFeed(string url)

   
34         {

   
35             string u = Server.UrlDecode(url);

   
36             System.Net.WebRequest wr = System.Net.WebRequest.Create(u);

   
37             XmlDocument doc = new XmlDocument();

   
38             doc.Load( wr.GetResponse().GetResponseStream() );

   
39             return doc;

   
40         }


   
41 

   
42         private XmlDocument GetError()

   
43         {

   
44             XmlDocument doc = new XmlDocument();

   
45             doc.LoadXml(“Invalid url“);

   
46             return doc;

   
47         }


   
48 

   
49     }


   
50 }


You�ll notice in the �GetExternalFeed� method, I�m UrlDecoding the
querystring parameter to create a valid url from the string.  Make
sure to use the javascript �escape
function on your querystring parameter before making the request,
otherwise any �&� in the url will be lost and your url will be
invalid.

So thats all there is to it, now you can get xml feeds from anywhere on the web!

转载于:https://www.cnblogs.com/joeliu/archive/2008/06/17/1224247.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值