Luftwaffe Luftwaffe - 2 days ago 4
ASP.NET (C#) Question

Call classic ASP function from ASPX

I am working on an old web application where pages were written in

classic ASP
and are wrapped in
aspx
pages using
Iframes
. I am rewriting one of those pages in
ASP.NET
(using C#) removing the dependency on Iframes altogether. The
page_to_rewrite.asp
call many other functions present in other ASP pages in the same application.

I am facing difficulty calling those ASP functions from aspx.cs. I tried to use WebClient class like this:

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();
}


Every request coming to this application is checked for a valid session cookie using an IIS HTTP module and if its not present user is redirected to login page. Now when I call this ASP page url from aspx I get the login page of my application as the response as no session cookie is present.

Can anyone please kindly suggest how can I call the ASP methods successfully.

Answer

As told by @Schadensbegrenzer in the comment I just had to pass the cookie in the request header like this:

    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();
}

In other similar questions on StackOverflow some people have suggested to also include User-Agent in the request header if you are getting blank output from the asp page as some web servers require this value in the request headers. See if it helps in your case. Mine worked even without it.

Also you will have to handle the request in your ASP page something like this:

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

Hope it helps!

Comments