Click here to Skip to main content
13,346,708 members (22,699 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

64.1K views
2 bookmarked
Posted 11 Oct 2013

ASP.NET Proxy Page – Used for Cross Domain Requests from AJAX and JavaScript

, 11 Oct 2013
Rate this:
Please Sign up or sign in to vote.
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain

Editorial Note

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull content or data from www.bing.com.

One way to overcome this issue is by using a server-side proxy on the site running the JavaScript code. There is already are well documented PHP solutions on the web, however I couldn’t find very many .NET-based solutions. This simple C# code takes the URL passed to it through the URL encoded query string, retrieves the content of the URL and outputs it as if it were content on the site.

Proxy.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;

namespace Proxy
{
    public partial class _Proxy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string proxyURL = string.Empty;
            try
            {
                proxyURL = HttpUtility.UrlDecode(Request.QueryString["u"].ToString());
            }
            catch { }

            if (proxyURL != string.Empty)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(proxyURL);
                request.Method = "GET";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode.ToString().ToLower() == "ok")
                {
                    string contentType = response.ContentType;
                    Stream content = response.GetResponseStream();
                    StreamReader contentReader = new StreamReader(content);
                    Response.ContentType = contentType;
                    Response.Write(contentReader.ReadToEnd());
                }
            }
        }
    }
}

Proxy.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Proxy.aspx.cs" Inherits="Proxy._Proxy" % >

The Proxy.aspx page is simply blank except for the Page tag. When passing the URL to the query string, it is important that it is URL encoded. This helps to prevent query strings of the remote site URL from interfering with the Proxy page.

 

Example Usage

yoursite.com/proxy.aspx?u=http%3a%2f%2fwww.google.com

 

Originally posted: http://www.johnchapman.name/aspnet-proxy-page-cross-domain-requests-from-ajax-and-javascript/

 

Add all line of code in try catch because error can occur in any line of code

    protected void getWebRequest()
    {

        try
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.msn.com");
            request.Method = "GET";
            request.Proxy = null;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string contentType = response.ContentType;
            Stream content = response.GetResponseStream();
            StreamReader contentReader = new StreamReader(content);
            Response.ContentType = contentType;
            Response.Write(contentReader.ReadToEnd());

        }
        catch (Exception ee)
        {

            Response.Write(ee.Message.ToString());
        }
    }

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ASP.NET Community
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Collaborative Group

764 members


You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 11 Oct 2013
Article Copyright 2013 by ASP.NET Community
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid