Ajax cross-origin proxy written in ASP. NET

Source: Internet
Author: User

I used ajax to write a WebGIS of ArcIMS. During the test, the data of other cities on the network was linked. Of course, the cross-origin problem is inevitable. The essence of Ajax cross-origin is JavaScript.ProgramOnly allow access to the data in the domain, while cross-domain access is restricted. A warning is displayed in IE, And it is terminated directly in ff, therefore, my WebGIS cannot be used in FF before cross-domain resolution (if the data is still available in the local domain ).

Now, the cross-origin solution is simple, that is, you can create a program in this domain that does not use the xhr object to send requests and receive responses. Therefore, ASP, ASPnet, JSP, or servlet can all take on this responsibility. The following is an ASPnet proxy I wrote:

Using system; using system. data; using system. text; using system. io; using system. configuration; using system. web; using system. web. security; using system. web. ui; using system. net; public partial class Proxy: system. web. UI. page {protected void page_load (Object sender, eventargs e) {string url = request ["url"]. tostring (); string clientversion = request ["clientversion"]. tostring (); string customservice = ST Ring. Empty; string url = string. Empty; If (request ["customservice"]! = NULL) // note that the form parameter {customservice = request ["customservice"] is not required. tostring (); url = URL + "& customservice =" + customservice + "& clientversion =" + clientversion;} else {url = URL + "& clientversion =" + clientversion ;} string ArcXML = request ["ArcXML"]. tostring (); byte [] postbytes = encoding. utf8.getbytes (ArcXML); string result = string. empty; httpwebrequest httpwreq = (httpwebrequest) webrequest. create (URL); httpwreq. method = "Post"; httpwreq. contenttype = "application/X-WWW-form-urlencoded"; httpwreq. contentlength = postbytes. length; using (Stream reqstream = httpwreq. getrequeststream () {reqstream. write (postbytes, 0, postbytes. length);} httpwebresponse httpwresp = (httpwebresponse) httpwreq. getresponse (); streamreader reader = new streamreader (httpwresp. getresponsestream (), encoding. utf8); Result = reader. readtoend (); // httpwresp. close (); response. write (result); response. end ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.