【问题标题】:Call classic ASP function from ASPX从 ASPX 调用经典的 ASP 函数
【发布时间】:2016-12-01 14:51:59
【问题描述】:

我正在开发一个 Web 应用程序,其中页面是用classic ASP 编写的,并使用Iframes 包装在aspx 页面中。我正在重写ASP.NET(使用C#)中的其中一个页面,完全消除了对Iframes的依赖。 page_to_rewrite.asp 调用同一应用程序中其他 ASP 页面中的许多其他函数。
我在从 aspx.cs 调用这些 ASP 函数时遇到了困难。我尝试像这样使用 WebClient 类:

using (WebClient wc = new WebClient())
{
            Stream _stream= wc.OpenRead("http://localhost/Employee/finance_util.asp?function=GetSalary?EmpId=12345");
            StreamReader sr= new StreamReader(_stream);
            string s = sr.ReadToEnd();
            _stream.Close();
            sr.Close();
}  

使用 IIS HTTP 模块检查到达此应用程序的每个请求是否有有效的会话 cookie,如果不存在,则将用户重定向到登录页面。现在,当我从 aspx 调用这个 ASP 页面 url 时,我得到了我的应用程序的登录页面作为响应,因为没有会话 cookie 存在。

任何人都可以请建议我如何成功调用 ASP 方法。

【问题讨论】:

  • 在您的 GET 请求中,您没有发送所需的 cookie。确保将 cookie 添加到标题中。 stackoverflow.com/questions/1777221/…
  • 经典 ASP 和 ASP.net 看不到对方的会话变量
  • finance_util.asp?function=GetSalary?EmpId=12345 这个查询字符串看起来不对 - 应该是 finance_util.asp?function=GetSalary&EmpId=12345
  • 哦.. 感谢您指出这一点。但是执行还没有达到这一点。由于缺少会话 cookie,对 Web 应用程序的任何请求都会被 HTTP 处理程序停止。

标签: c# asp.net asp-classic


【解决方案1】:

正如@Schadensbegrenzer 在评论中所说,我只需要像这样在请求标头中传递 cookie:

    using (WebClient wc = new WebClient())
{
    wc.Headers[HttpRequestHeader.Cookie] = "SessionID=" + Request.Cookies["SessionID"].Value;
    Stream _stream= wc.OpenRead("http://localhost/Employee/finance_util.asp?function=GetSalary&EmpId=12345");
    StreamReader sr= new StreamReader(_stream);
    string s = sr.ReadToEnd();
    _stream.Close();
    sr.Close();
}

在 StackOverflow 上的其他类似问题中,如果您从 asp 页面获得空白输出,一些人建议在请求标头中也包含 User-Agent,因为某些 Web 服务器在请求标头中需要此值。看看它是否对你的情况有帮助。我的即使没有它也能工作。

您还必须在ASP page 中处理请求,如下所示:

Dim param_1
Dim param_2
Dim output

param_1 = Request.QueryString("function")
param_2 = Request.QueryString("EmpId")

 If param_1 = "GetSalary" Then
    output = GetSalary(param_2)
    response.write output
 End If

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2012-12-29
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多