asp.net xmlhttp cross-domain access to implement code _ practical tips

Source: Internet
Author: User
Due to browser security restrictions, Cross-domain access to a network connection is not allowed. We cannot use Ajax directly on the browser side to access resources across domains, but there is no such cross-domain security restriction on the server side. So we just need to get the server side to help us with cross-domain access, and then get the results directly from the server-side Cross-domain access. Share my solution and hope it will help. ^_^
certcheck.aspx:
Copy Code code as follows:

Using MSXML2;
Namespace CA2
{
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:
Copy Code code as follows:

<?xml version= "1.0"?>
<root>
</root>

PS: You can write parameters directly in Xh.send (),
Certcheck.aspx:
Copy Code code as follows:

Using System.Net;
Using System.IO;
Namespace CA2
{
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
Copy Code code as follows:

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

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.