Cross-origin access and page proxy of xmldom/XMLHTTP

Source: Internet
Author: User
We know that Microsoft's xmldom/XMLHTTP component can be called by using JavaScript on the page to facilitate various remote resources on the page, such as webpage and XML data. However, due to the security restrictions of IE by default, we cannot access data sources in different domains through xmldom/XMLHTTP. In other words, www.sitea.com cannot access pages in www.siteb.com. We can use a simple local page proxy to implement this function. The new process will be: sitea.com/local.aspx (JavaScript)-> sitea.com/agent.aspx? Url = siteb.com/data.xml-> siteb.com/data.xml

Code It is very simple. The post method is supported, but other parameters in the URL are not processed at present in the get method. <% @ Page Language = " C # " Classname = " Pageagent " Validaterequest = " False "   %>

<% @ Import namespace = " System. Io "   %>
<% @ Import namespace = " System. net "   %>

< Script Type = " Text/C # " Runat = " Server " >
Protected   Override   Void Onload (eventargs E)
{
Response. Clear ();

String URL = Request. querystring [ " URL " ];
If ( Null   = URL | URL. Trim (). Length =   0 )
{
Response. End ();
Return;
}

Try
{
Webrequest request = Webrequest. Create (URL );
Request. Method = Request. requesttype;
Request. contenttype = Request. contenttype;

Int Reads =   0 ;
Byte [] Buffer =   New   Byte [ 512 ];

If (Request. requesttype. Equals ( " Post " , Stringcomparison. invariantcultureignorecase ))
{
If ( Null   ! = Request. Form) {
Using (Stream s = Request. getrequeststream ())
{
While (Reads = Request. inputstream. Read (buffer, 0 , Buffer. Length )) >   0 )
{
S. Write (buffer,0, Reads );
}
S. Flush ();
}
}
}

Webresponse response = Request. getresponse ();
Response. contenttype = Response. contenttype;
Using (Stream s = Response. getresponsestream ())
{
While (Reads = S. Read (buffer, 0 , Buffer. Length )) >   0 )
{
Response. outputstream. Write (buffer,0, Reads );
}
}

Response. Close ();
}
Catch
{
}

Response. End ();
}
</ Script >

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.