Ajax cross-origin access proxy File Download (asp, php, asp.net)

Source: Internet
Author: User
Recently, I encountered cross-domain ajax access. Finally, I used the Application Proxies method to solve the problem, that is, place a proxy file in the domain (depending on the development languages supported by the domain, select asp, asp.net, or others). This proxy file sets the url parameter (QueryString) go to the corresponding page of the target domain to get the html code and output it. Ajax directly accesses this proxy file for cross-origin purposes.
The asp.net-based cross-origin access proxy file c # code is as follows: <% @ Page Language = "C #" AutoEventWireup = "true" ResponseEncoding = "UTF-8" %>
<% @ Import Namespace = System. Net %>
<% @ Import Namespace = System. IO %>

<Script runat = "server">
Protected override void OnLoad (EventArgs e)
{
Base. OnLoad (e );

String sourceUrl = "http://devspy.net ";
This. Page. Response. Write (TransferHtmlPage (string. Concat (sourceUrl ,"? ", This. Page. Request. QueryString )));
}

Public string TransferHtmlPage (string url)
{
String result = string. Empty;
Try
{
HttpWebRequest request = (HttpWebRequest) WebRequest. Create (url );
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
StreamReader reader = new StreamReader (response. GetResponseStream (), Encoding. UTF8 );
Result = reader. ReadToEnd ();
}
Catch (Exception ex)
{
Return string. format (@ "<p style = 'color: red; text-align: center; '> An error occurred while obtaining the file content on the server: {0} </p>", ex. message );
}

If (! CheckVersionWaterMark (result ))
Return @ "<p style = 'color: red; text-align: center; '> the version watermark is invalid. Contact the related technical staff. </P> ";

Return result;
}

Public bool CheckVersionWaterMark (string inputString)
{
Return true; // the watermark is not verified.
// String pattern = "WaterMark ";
// Return Regex. IsMatch (inputString, pattern, RegexOptions. IgnoreCase );
}

</Script>


In addition, the Implementation Based on asp and php is not listed. If you are interested, you can download a compressed package containing these three files:
Http://files.cnblogs.com/cncxz/ajaxProxy.rar

Related Article

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.