ASP.NET XmlHttp跨域访问实现代码
程序员文章站
2022-07-06 17:53:24
原来由于浏览器的安全限制,网络连接的跨域访问时不被允许的。我们不能在浏览器端直接使用ajax来跨域访问资源,但是在服务器端是没有这种跨域安全限制的。所以,我们只需要让服务器...
原来由于浏览器的安全限制,网络连接的跨域访问时不被允许的。我们不能在浏览器端直接使用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();
}