2

I'm tring to write a little program to download a PDF from a newspaper website that requires a login. I'm using this library found on stackoverflow: http://pastebin.com/RPNU39vF

And I call it with this code:

   private void backupFunzionante()
    {
        String postData = "log=MYEMAIL@gmail.com&pwd=MYPASSWORD";
        myWeb.RequestManager manager = new myWeb.RequestManager();

        String uri = "http://shop.ilfattoquotidiano.it/login/?action=login";
        HttpWebResponse response;

        response = manager.SendPOSTRequest(uri, postData, null, null, true);
        String strResp = response.StatusCode.ToString();
        label1.Text += "###";

        Uri pdfUrl = new Uri("http://pdf.ilfattoquotidiano.it/openpdf/?n=20120927");
        response = manager.SendPOSTRequest("http://pdf.ilfattoquotidiano.it/openpdf/?n=20120927", "", null, null, true);
        long size = response.ContentLength;

        Stream downloadStream = response.GetResponseStream();

        using (Stream file = File.OpenWrite("C:\\f.pdf"))
        {
            CopyStream(downloadStream, file);
        }
    }

    public static void CopyStream(Stream input, Stream output)
    {
        byte[] buffer = new byte[8 * 1024];
        int len;
        while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, len);
        }
    }

This code works perfectly if run under .NET, while it returns a empty file if called under mono.

I think the problem should be in http post request, because size in long size = response.ContentLength; is zero.

Why there is this difference between the two executables? What I can do to have a fully portable application (I would like to use it also under linux because it is my primary OS)

Thank you for your help.

Neopard
  • 41
  • 4
  • 1
    What status code do you get back under Mono? If I had to guess it's probably something to do with the auth cookie, try passing the `postData` with your PDF request and see if it works. Also, I probably wouldn't be 100% comfortable using the library you have linked to it's got a bug in it & doesn't seem the most robust. – James Sep 29 '12 at 10:32
  • Thank you for your reply. Under .NET: 1) after login requeste response.statuscode.ToString() is "Redirect" and response.StatusDescription is "Found" 2) after download request response.statuscode.ToString() is "Redirect" and response.StatusDescription is "Found" Under Mono: 1) after login requeste response.statuscode.ToString() is "Found" and response.StatusDescription is "Found" 2) after download request response.statuscode.ToString() is "Found" and response.StatusDescription is "OK" – Neopard Sep 29 '12 at 12:45
  • I've searched the library for the bug you mentioned and i found that allowRedirect is always set to false, i've corrected it but it does not change... Is this the bug you were talking about? – Neopard Sep 29 '12 at 12:49
  • yeah that was the bug I seen. So did you try passing the credentials with the PDF request? – James Sep 29 '12 at 20:54
  • I was sure to have reply to your last comment yesterday night, but i don't see my post... sorry. I don't understand what you mean: in the library we have: `// Set cookie container to maintain cookies request.CookieContainer = cookies;` so i think that credentials are passed along with second request. moreover, the credentials are not passed in a different way when run under .NET and it works. Can you please explain me what did you mean by "passing the credentials with the PDF request"? (i'm really new in network programming, i'm learning frome the code i see around) – Neopard Sep 30 '12 at 07:57
  • "*so I think that the credentials are passed along with the second request*" - my suspicion is they aren't, which is why I was suggesting you try simply send that information along with your second request. However, that being said the main difference is under windows your requests are being redirected but under Mono they aren't - doesn't make sense to me, it should be the same under both as the server hasn't changed. I suggest you download a HTTP debugger like [Fiddler](http://fiddler2.com/fiddler2/) to get a better look at the requests. – James Sep 30 '12 at 08:21
  • If i pass postData along with the seconde request it stops working also under .NET (it returns a web page, not a pdf). I should try as soon as possible with the HTTP debugger. Thnaks for the suggestion. – Neopard Sep 30 '12 at 14:31

1 Answers1

0

Mono for Windows has bugs that may not be present in Mono for Linux/Macintosh, because Mono for Windows has never been a priority (because for that OS there is already Micosoft's .NET).

I recommend you testing with Mono for Linux first. It may work fine. (For that, obviously you'll need to replace "C:\" with a native unix path.)

knocte
  • 16,941
  • 11
  • 79
  • 125
  • Hi knocte, thank for your answer. I tried under a linux in virtual machine (in virtual machine only because i don't want to reboot while i'm programming on Visual Studio ) – Neopard Sep 29 '12 at 22:56
  • I intended to say that i was trying under linux while copy-pasting the status code in my first post. And, for sake of curiosity, the file was created in the same directory, named "C:\f.pdf" so i've learned a new thing: under linux you can use : and \ as characters of file names – Neopard Sep 30 '12 at 07:52