English 中文(简体)
getting XML from other domain using ASP.NET
原标题:

I m fairly new to ASP.NET. And I was wondering how I could go about getting xml from a site (Kuler s API in this case), and then post the result using AJAX?

So what I want here, is to be able to do a query to Kuler s API. The URL would be something like "http://kuler-api.adobe.com/rss/search.cfm?query="+queryVariable
Then send the resulting xml back to JS in some way.

Any pointers would be appreciated (:

最佳回答

What you ll need to do is have a handler that will perform the request for the XML and send it back to the browser using AJAX. It will act as an intermediary between server and client, and you won t have to worry about cross-domain policies.

This is what I do on one of my sites. I have a handler (let s call it proxy.ashx) that I call from a jQuery AJAX request. The proxy.ashx simply performs a WebClient.DownloadString action on the remote URL and sends the remote response (the XML) back to the client-side.

问题回答

I think that Tim said enough, but what I would like to add is how you could do the server side request:

            XmlDocument doc = new XmlDocument();
            HttpWebRequest r = (HttpWebRequest)HttpWebRequest.Create("http://kuler-api.adobe.com/rss/search.cfm?query="+queryVariable);
            r.Method = "POST";


            using (Stream writeStream = r.GetRequestStream())
            {
                UTF8Encoding encoding = new UTF8Encoding();
                byte[] bytes = encoding.GetBytes(bodyRequest);
                writeStream.Write(bytes, 0, bytes.Length);
            }
            try
            {
                using (HttpWebResponse response = (HttpWebResponse)r.GetResponse())
                {

                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                        {
                            doc.Load(readStream);
                        }
                    }

                }
            }
            catch (WebException ex)
            {
                //Handle exception
            }

I d do the whole thing in Javascript using Jquery s ajax library, if possible. It s very simple to use and you don t have to worry about getting the XML from server to client that way.

Write a .net webservice (.asmx) that encapsulate the cross domain call, then call that service with AJAX.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签