能否用ASP.net写一个在服务器端访问其他网站的asp文件,返回asp执行后的结果?
很惭愧,我还不懂ASP.net。虚心向各位ASP.net版的XDJM们请教。
我用ASP写了一个网页,返回结果只是字符串的“1”或者“0”,连HTML标签也没有。
如果用客户端的AJAX访问就简单了。可是我希望用ASP.net写的网页在它的服务器端能够使用XMLHttpRequest或其他方式访问我的ASP网页,将ASP返回的字符串(1或0)保存在ASP.net的Session里。
要求:
1、最好写1个函数,名称是CheckAJAX,参数是访问的ASP的URL。函数内部用XMLHttpRequest或其他方式访问URL,返回ASP网页的结果,不使用异步访问。
如果用XMLHttpRequest,open方法的三个参数是 "GET "、URL、false。
2、这个ASP.net文件也需要代入两个参数:MyID和MyCode。这两个参数是提交到这个ASP.net文件的表单中文本框的名称。
用ASP.net实现类似下面代码的功能(我的是ASP代码):
/****开始****/
/*表单中名称为MyCode的文本框中输入了文字,asp代码*/
<%
If Request.Form( "MyCode ") <> " " Then
/*执行CheckAJAX函数,代入URL参数,将返回内容存入Session变量 "Result "中。
/*需要加入时间戳,使不能使用缓存。ASP的Timer可以返回从半夜零点开始到现在的毫秒数
Session( "Result ") = CheckAJAX( "http://www.AAAAA.com/Check.asp " _
"?TimeStamp= " & Timer & _
"&ID= " & Request.Form( "MyID ") & _
"&Code= " & Request.Form( "MyCode "))
End If
%>
<p> 这是HTML代码。URL返回结果是: <%=Session( "Result ")%> </p>
/*ASP代码*/
<%
If Session( "Result ") = "1 " Then
Response.Write( " <p> 是1 </p> ")
Else
Response.Write( " <p> 不是1 </p> ")
End If
%>
/****结束****/
3、请写出完整的包括HTML的代码。就是包括:
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> 无标题文档 </title>
</head>
<body>
</body>
</html>
非常感激!
[解决办法]
我确实不太会手写
test.aspx内容
<%@ Page language= "c# " Codebehind= "test.aspx.cs " AutoEventWireup= "false " Inherits= "TestWeb.test " %>
<HTML>
<HEAD>
</HEAD>
<body>
<form id= "Form1 " method= "post " runat= "server ">
<P> MyID:
<asp:TextBox id= "txtMyID " runat= "server "> </asp:TextBox> </P>
<P> MyCode:
<asp:TextBox id= "txtMyCode " runat= "server "> </asp:TextBox> </P>
<P> <asp:Button id= "btnOK " runat= "server " Text= "Button "> </asp:Button> </P>
<P> <asp:Label id= "labInfo " runat= "server "> </asp:Label> </P>
</form>
</body>
</HTML>
*****************************************************************************
test.aspx.cs内容
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
namespace TestWeb
{
/// <summary>
/// test 的摘要说明。
/// </summary>
public class test : System.Web.UI.Page
{
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
protected System.Web.UI.WebControls.TextBox txtMyID;
protected System.Web.UI.WebControls.TextBox txtMyCode;
protected System.Web.UI.WebControls.Button btnOK;
protected System.Web.UI.WebControls.Label labInfo;
private void Page_Load(object sender, System.EventArgs e)
{
}
private void btnOK_Click(object sender, System.EventArgs e)
{
string myCode=txtMyCode.Text.Trim();
if(myCode!=string.Empty)
{
string result=CheckAJAX( "http://www.AAAAA.com/Check.asp "
+ "?TimeStamp=xxx&ID= " + txtMyID.Text.Trim()
+ "&Code= " + myCode);
Session[ "Result "] = result;
labInfo.Text=result== "1 "? "是1 ": "不是1 <br> "+result;
}
}
private string CheckAJAX(string url)
{
HttpWebRequest req = null;
HttpWebResponse res = null;
try
{
req = (HttpWebRequest)WebRequest.Create(url);
res = (HttpWebResponse)req.GetResponse();
return new StreamReader(res.GetResponseStream()).ReadToEnd();
}
catch(Exception ex)
{
return ex.Message;
}
finally
{
if(res != null)
res.Close();
}
}
}
}
