ASP. NET background code for XmlHttp cross-origin access

Source: Internet
Author: User

Recently implemented projectsXmlHttpThe POST method is used to update the data on the page of another server, but IE will propose that "the page is accessing information beyond its control scope. Do you want to continue ?" And other warning information, but it is directly forbidden on other browsers. GOOGLE is originally a cross-origin access problem of XmlHttp, and found a lot of information, saying that it provides a lot of solutions, but it is useless.

Originally, due to browser security restrictions, cross-domain access to network connections is not allowed. We cannot directly useAJAXCross-origin access to resources, but there is no such cross-origin Security Restriction on the server side. Therefore, we only need to ask the server side to help us with "cross-origin access" and then directly obtain the results of "cross-origin access" on the server side. I hope it will be helpful to share my solutions. Pai_^

CertCheck. aspx:

Using MSXML2;
Namespace Ca 2
{
Public partial class CertCheck: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (Request ["resultid"]! = Null)
{
String s = Request ["resultid"];
String [] result = s. Split ('@');
Label1.Text = result [1];
System. Xml. XmlDocument xmlDoc = new System. Xml. XmlDocument ();
XmlDoc. Load ("VBR. xml ");
System. Xml. XmlElement RegNode = xmlDoc. CreateElement ("Id ");
RegNode. InnerText = result [0];
XmlDoc. DocumentElement. AppendChild (RegNode );
RegNode = xmlDoc. CreateElement ("Serial ");
RegNode. InnerText = result [1];
XmlDoc. DocumentElement. AppendChild (RegNode );
MSXML2.XMLHTTP xh = new MSXML2.XMLHTTPClass ();
Xh. open ("POST", "http: // 222.19.211.119/CAProcess. aspx", false, null, null );
Xh. setRequestHeader ("Content-Type", "text/xml ");
Xh. setRequestHeader ("Content-Type", "gb2312 ");
Xh. send (xmlDoc. InnerXml );
If (xh. readyState = 4)
{
If (xh. status = 200)
{
Label1.Text + = "###" + xh. responseText;
}
}
}
}
}
}

VBR. xml:

<? Xml version = "1.0"?>
<Root>
</Root>

PS: You can directly write parameters in xh. send,

CertCheck. aspx:

Using System. Net;
Using System. IO;
Namespace Ca 2
{
Public partial class CertCheck: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (Request ["resultid"]! = Null)
{
String s = Request ["resultid"];
String [] result = s. Split ('@');
Label1.Text = result [1];
String param = "Id =" + result [0] + "& Serial =" + result [1];
Byte [] bs = Encoding. ASCII. GetBytes (param );
HttpWebRequest req = (HttpWebRequest) HttpWebRequest. Create ("http: // 222.19.211.119/CAProcess. aspx ");
Req. Method = "POST ";
Req. ContentType = "application/x-www-form-urlencoded ";
Req. ContentLength = bs. Length;
Stream reqStream = req. GetRequestStream ();
ReqStream. Write (bs, 0, bs. Length );
ReqStream. Close ();
HttpWebResponse myResponse = (HttpWebResponse) req. GetResponse ();
StreamReader reader = new StreamReader (myResponse. GetResponseStream (), Encoding. GetEncoding ("GB2312 "));
String content = reader. ReadToEnd ();
Reader. Close ();
MyResponse. Close ();
Label1.Text + = "###" + content. ToString ();
}
}
}
}

CAProcess. aspx

Protected void Page_Load (object sender, EventArgs e)
{
Int Id = int. Parse (Request. Form ["Id"]);
String Serial = Request. Form ["Serial"];
//
Response. Write (Id. ToString () + "" + Serial );
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.