0

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.

Saurabh R S
  • 3,037
  • 1
  • 34
  • 44
  • 1
    In your GET request you dont send the required cookie. Make sure that you add the cookie to the header. http://stackoverflow.com/questions/1777221/using-cookiecontainer-with-webclient-class – Schadensbegrenzer Nov 25 '16 at 09:53
  • Classic ASP and ASP.net can't see each other's session variables – John Nov 25 '16 at 19:24
  • 2
    `finance_util.asp?function=GetSalary?EmpId=12345` this querystring looks wrong - should be `finance_util.asp?function=GetSalary&EmpId=12345` – Tim Williams Nov 26 '16 at 07:26
  • Oh.. Thanks for pointing that out. But the execution is not reaching this point. Any request to the web app is stopped by HTTP handler because of the absence of session cookie. – Saurabh R S Nov 26 '16 at 17:38

1 Answers1

0

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!

Saurabh R S
  • 3,037
  • 1
  • 34
  • 44