Solved

C# to  ASP   translation

Posted on 2013-11-25
4
448 Views
Last Modified: 2013-12-10
Can someone show the translation from .NET to classic ASP , either Javascript or VBscript
 for this code that logins in, sets a cookie, then and gets a PDF from a remote web server?  Thanks in advance.

 GetPdf(string page_link, string filename)
    {

        string username = "myusername";
        string pass = "mypassword";
        string codePageName = "UTF-8";

        string loginPostData = "userName=" + username + "&Password=" + pass;
        HttpWebRequest httpRequest = WebRequest.Create("http://sitename/auth.aspx") as HttpWebRequest;
        httpRequest.AllowAutoRedirect = false;
        httpRequest.Method = "POST";
        httpRequest.ContentType = "application/x-www-form-urlencoded";

        Encoding enc = Encoding.GetEncoding(codePageName);
        byte[] bytes = enc.GetBytes(loginPostData);
        httpRequest.ContentLength = bytes.Length;
        Stream reqStream = httpRequest.GetRequestStream();
        reqStream.Write(bytes, 0, bytes.Length);
        reqStream.Flush();

        HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;
        string cookie = response.Headers["Set-Cookie"];

        WebClient webclient = new WebClient();
        webclient.Headers.Add("Cookie", " " + cookie.Replace("HttpOnly,", "HttpOnly; "));
        webclient.DownloadFile(pagelink, filename);
       
        Response.ContentType = "application/pdf";
        Response.Clear();
        Response.TransmitFile(filename);
        Response.End();
}
0
Comment
Question by:awalkinthepark
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
kevp75 earned 500 total points
ID: 39677558
Modify to suit your needs, but we won't help unless you try to do it yourself...

public function SaveFileFromUrl(Url, FileName)
    dim objXMLHTTP, objADOStream, objFSO

    Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.3.0")

    objXMLHTTP.open "GET", Url, false
    objXMLHTTP.send()

    If objXMLHTTP.Status = 200 Then 
        Set objADOStream = CreateObject("ADODB.Stream")
        objADOStream.Open
        objADOStream.Type = 1 'adTypeBinary

        objADOStream.Write objXMLHTTP.ResponseBody
        objADOStream.Position = 0 'Set the stream position to the start

        Set objFSO = Createobject("Scripting.FileSystemObject")
        If objFSO.Fileexists(FileName) Then objFSO.DeleteFile FileName
        Set objFSO = Nothing

        objADOStream.SaveToFile FileName
        objADOStream.Close
        Set objADOStream = Nothing

        SaveFileFromUrl = objXMLHTTP.getResponseHeader("Content-Type")
    else
        SaveFileFromUrl = ""
    End if

    Set objXMLHTTP = Nothing
end function

Open in new window


Let us know how you make out, and if you have any issues with it...
0
 

Author Comment

by:awalkinthepark
ID: 39678462
Sure, wasn't expecting anything more than the fundamental pieces.
Appreciate the help.
0
 
LVL 25

Expert Comment

by:kevp75
ID: 39678562
That should definately get you going.
0
 

Author Closing Comment

by:awalkinthepark
ID: 39709623
Thanks - can work with this.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now