ASP. net xmlhttp cross-origin access implementation code

Source: Internet
Author: User

Originally, due to browser security restrictions, cross-domain access to network connections is not allowed. We cannot directly use Ajax on the browser side to access resources across domains, but there is no such cross-domain 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: CopyCode The Code is as follows: 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: Copy codeThe Code is as follows: <? XML version = "1.0"?>
<Root>
</Root>

PS: You can directly write parameters in XH. Send,
Certcheck. aspx:Copy code The Code is as follows: 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 Copy codeThe Code is 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 ();
}

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.