Cross Browser Proxy

I am developing client-side Javascript that uses some JSON web services in a different domain. I read that some browsers do not allow cross-domain scripts and that I have to create a proxy server on my local server to serve the data.

Can someone point me to a simple example of how to do this in ASP.Net?

+3
source share
5 answers

Perhaps you can avoid the proxy server using a technique such as JSONP . Assuming that the web service you are talking to supports JSONP (for example, Flickr or Twitter both offer the JSONP API), or you control the data sent by the web service, you can send JSON data between domains using a library with JSONP functions.

For example, in jQuery you can make a remote JSON call:

jQuery.getJSON("http://www.someothersite.com/webservice?callback=?", function(result)
{
    doStuffWithResult(result);
});

Since the call belongs to a different domain, jQuery automatically uses some tricks to make a cross-domain call. jQuery will automatically replace? in the url with the name of the callback function that the web service can use to format the returned JSON data.

-, JSONP, , " ", , . , JSON, . , "jsonp2342342", , - :

jsonp2342342({key: value, key2: value});

-, , JSONP, , .

+3

, - - , IIS - "" .

, #.NET

, AJAX

+6

.NET :

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());
                }
            }
        }
    }
}

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

+2

, w3c xmlHTTPRequest, , , ...

0

, .

SomeAjaxAbstraction.Request('proxyScript', {
    parameters: {
        address: 'http://somewhere.com/someapi?some=query'
    }
});

proxyScript:

var address = GET['address'];
if(ValidUrl(address) && ConnectionAllowed(address)) {
    // Validating address and whitelisting services is an exercise to the reader
    var response = SomeHttpGetFunction(address);
    echo XssAndBadStuffFilter(response);
} else {
    // Handle errors
}
0

Source: https://habr.com/ru/post/1698732/


All Articles