ASP.NET XmlHttp跨域訪問實現代碼

來源:互聯網
上載者:User

原來由於瀏覽器的安全限制,網路連接的跨域訪問時不被允許的。我們不能在瀏覽器端直接使用AJAX來跨域訪問資源,但是在伺服器端是沒有這種跨域安全限制的。所以,我們只需要讓伺服器端幫我們完成“跨域訪問”的工作,然後直接擷取伺服器端“跨域訪問”的結果就可以了。分享下我的解決辦法,希望能有所協助。^_^
CertCheck.aspx: 複製代碼 代碼如下: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: 複製代碼 代碼如下:<?xml version="1.0"?>
<root>
</root>

PS: 在xh.send()中可以直接寫參數,
CertCheck.aspx: 複製代碼 代碼如下: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 複製代碼 代碼如下: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();
  }

相關關鍵詞:
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.