简体   繁体   English

System.net,mono和.Net之间的httpPOST兼容性

[英]System.net, httpPOST compatibility between mono and .Net

I'm tring to write a little program to download a PDF from a newspaper website that requires a login. 我正在尝试编写一个小程序,从需要登录的报纸网站下载PDF。 I'm using this library found on stackoverflow: http://pastebin.com/RPNU39vF 我正在使用在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. 如果在.NET下运行,此代码将完美工作,而在mono下调用时,它将返回一个空文件。

I think the problem should be in http post request, because size in long size = response.ContentLength; 我认为问题应该出在http发布请求中,因为长整型的大小= 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) 我有一个完全可移植的应用程序可以做什么(我想在Linux下也使用它,因为它是我的主要操作系统)

Thank you for your help. 谢谢您的帮助。

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). 适用于Windows的Mono具有Linux / Macintosh上的Mono中可能没有的错误,因为适用于Windows的Mono从未被优先考虑(因为该操作系统已经存在Micosoft的.NET)。

I recommend you testing with Mono for Linux first. 我建议您首先使用Mono for Linux进行测试。 It may work fine. 它可能工作正常。 (For that, obviously you'll need to replace "C:\\" with a native unix path.) (为此,显然,您需要用本机unix路径替换“ C:\\”。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM