Wednesday, March 21, 2012

URL WebRequest & cross domain web services

Initially, I would like make a web services call with cross domain (ajax). Since RC doesnt support it at the moment. So, I'm looking for an alternative.

- One is a bridge method,http://forums.asp.net/thread/1510827.aspx
But when will CTP support it?

- Another way to use WebRequest. However, there is an error "Access is denied" when I trying to access standard URL. ( change L17 to L16)

Please take a look at L16, L17

1<%@dotnet.itags.org. Page Language="C#" %>23<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">45<html xmlns="http://www.w3.org/1999/xhtml" >6<head runat="server">7 <title>Web Request</title>8 <script type="text/javascript">9 function MakeWebRequest()10 {11 var displayElement = document.getElementById("ResultId");12 displayElement.innerHTML = "";13 wRequest = new Sys.Net.WebRequest();14 Sys.Net.WebRequestManager.add_completedRequest(On_WebRequestCompleted);1516 wRequest.set_url("http://www.asp.net");17 //wRequest.set_url("getTarget.htm");1819 Sys.Net.WebRequestManager.executeRequest(wRequest);20 }2122 function On_WebRequestCompleted(executor, eventArgs)23 {24 var displayElement = document.getElementById("ResultId");2526 if(executor.get_responseAvailable())27 {28 displayElement.innerHTML = "";29 displayElement.innerHTML += executor.get_responseData();30 }31 }32 </script>33</head>34<body>35 <form id="form1" runat="server">36 <asp:ScriptManager runat="server" ID="scriptManagerId"/>37 </form>3839 <h1>Make a Web request:</h1>40 <button id="Button1" title="adds and remove handlers, too" onclick="MakeWebRequest();">Web Request</button>41 <hr />42 <div id="ResultId" style="background-color:Aqua;"></div>43</body>44</html>45
Unfortunately, webrequest internally uses XmlHttpRequest which does not work for cross-domain calls for security reasons. BTW The web service proxy layer internally uses WebRequest so it does not work for that too. The CTP versions used to have an IFrameExecutor which mysteriously got removed in the latest CTP. The IFrameExecutor allowed cross-domain calls. Now as far as handling cross-domain calls you can do it from the server. For instance you can develop an Generic HTTP handler which will take the URL of the target cross-domain page and return its contents. Save the code of the following examples in Delegate.ashx in your web site. The you can invoke the request to the following url:

delegate.ashx?url=www.asp.net

The server will then download the contents ofwww.asp.net and return it. Here is the code of the handler:

------

<%@. WebHandler Language="C#" Class="LoadRssFeed" %>using System;using System.Web;using System.Net;public class LoadRssFeed : IHttpHandler{ private HttpContext _context; public HttpContext Context { get { return _context; } } public HttpResponse Response { get { return Context.Response; } } public HttpRequest Request { get { return Context.Request; } } public void ProcessRequest(HttpContext context) { this._context = context; string url = Request.QueryString["url"]; if (String.IsNullOrEmpty(url)) { Response.StatusCode = (int)HttpStatusCode.NotFound; return; } try { WebRequest request = WebRequest.Create(url); HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse(); if (!String.IsNullOrEmpty(webResponse.ContentEncoding)) { try { Response.ContentEncoding = System.Text.Encoding.GetEncoding(webResponse.ContentEncoding); } catch (ArgumentException ex) { System.Diagnostics.Trace.WriteLine(ex.ToString()); } } Response.ContentType = webResponse.ContentType; Response.StatusCode = (int)webResponse.StatusCode; Response.StatusDescription = webResponse.StatusDescription; byte[] buffer = new byte[4096]; System.IO.Stream stream = webResponse.GetResponseStream(); for (; ; ) { int read = stream.Read(buffer, 0, buffer.Length); if (read == 0) break; if (read > 0) Response.OutputStream.Write(buffer, 0, read); } } catch (ArgumentNullException ex) { Response.StatusCode = (int)HttpStatusCode.NotFound; System.Diagnostics.Trace.WriteLine(ex.ToString()); } catch (System.Security.SecurityException ex) { Response.StatusCode = (int)HttpStatusCode.Forbidden; System.Diagnostics.Trace.WriteLine(ex.ToString()); } catch (UriFormatException ex) { Response.StatusCode = (int)HttpStatusCode.BadRequest; System.Diagnostics.Trace.WriteLine(ex.ToString()); } catch (Exception ex) { Response.StatusCode = (int)HttpStatusCode.InternalServerError; System.Diagnostics.Trace.WriteLine(ex.ToString()); } } public bool IsReusable { get { return false; } }}

Thanks Rama Krishna, I would like to ask if there exists any methods so that

"the page sends some data to an external aspx page (using post and get method) and get result page in client side only using js or atlas client"

No comments:

Post a Comment