ASP.NET服务器端实现XmlHttp跨域访问
2011-05-06 23:46
363 查看
ASP.NET服务器端实现XmlHttp跨域访问怎样做?最近在做一个ajax的异步跨域名获取数据,最终结果当然是失败的,因为浏览器出于安全原因都不允许异步跨域名访问。为有出动ASP.NET服务器端实现XmlHttp跨域访问。
方法一、使用xml传递参数
CertCheck.aspx.cs文件
VBR.xml文件
方法二、直接传递参数
CertCheck.aspx文件
跨域名请求的CAProcess.aspx文件
方法一、使用xml传递参数
CertCheck.aspx.cs文件
]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>
方法二、直接传递参数
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(); }
相关文章推荐
- ASP.NET XmlHttp跨域访问实现代码
- ASP.NET XmlHttp跨域访问实现代码
- ASP.NET C# 用后台代码实现 跨域名访问 返回HTML代码
- ASP.NET MVC & WebApi 中实现Cors来让Ajax可以跨域访问 (转载)
- 使用ASP.Net创建代理实现Javascript跨域访问
- Asp.Net WebApi 启用CORS跨域访问指定多个域名
- [亲测]七步学会ASP.NET Core 2.0怎么发布/部署到Ubuntu Linux服务器并配置Nginx反向代理实现域名访问
- 如何通过js跨域调用ASP.NET Web API (请问如何实现在javascript中通过http get的方式跨域调用ASP.NET Web API?)
- asp.net 访问webapi跨域问题
- Asp.Net XmlHttp实现无刷新三联动下拉框
- 关于云服务器的ASP.Net MVC网站实现被外网访问
- ASP.NET Web API 跨域访问(CORS)
- Asp.Net实现记录历史访问人数和当前在线人数
- asp.net 真正实现完全跨域单点登录
- asp.net中解决json跨域访问问题
- .NET 中从一个类库中访问资源文件,以实现 ASP.NET 或 ASP.NET MVC 的国际化
- ASP.NET实现匿名访问控制
- 怎样在Asp.net中实现当前网站的访问用户数量统计
- ASP.NET中用XMLHTTP实现DropDownList无刷新联动
- ASP.NET Boilerplate 学习 AspNet Core2 浏览器缓存使用 c#基础,单线程,跨线程访问和线程带参数 wpf 禁用启用webbroswer右键菜单 EF Core 2.0使用MsSql/MySql实现DB First和Code First ASP.NET Core部署到Windows IIS QRCode.js:使用 JavaScript 生成