原来由于浏览器的安全限制,网络连接的跨域访问时不被允许的。我们不能在浏览器端直接使用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();
}
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();
}
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“ASP.NET XmlHttp跨域访问实现代码”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。