Resolved HttpRequest download PDF files

Gekidow

Member
Joined
Apr 20, 2023
Messages
12
Programming Experience
Beginner
Hello, I have a problem, my program reads an ODT file and downloads the links inside it, these links correspond to PDF files available on an intranet. The problem is that as a result of the program, I don't have 128 pdf that are downloaded but I have 128 files (which correspond well in terms of name to what I am supposed to have) without extension, and which are all 18 kb of size. My question is then the following: Why do I not have PDF files in output but files without extension as on the screenshot? Is it a redirect problem ? I also tried with DownloadFile method and i have the same result The System.Diagnostics.Process.Start(link); method works but I can't rename the files because the program only execute them and doesn't downloads them(the browser downloads them). PS : i'm on .NET 3.5 and Visual Studio 2010

résultatfichiers.png


Here is my code :
C#:
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;

namespace PrepareDocForExternalUse
{
    class Program
    {
        static void Main(string[] args)
        {
            // Prompts the user for the absolute path to an ODT file
            Console.WriteLine("Please enter the absolute path of an ODT file:");
            string odtFilePath = Console.ReadLine();

            // Read the contents of the ODT file
            byte[] content = File.ReadAllBytes(odtFilePath);
            MemoryStream ms = new MemoryStream();
            ms.Write(content, 0, content.Length);
            ZipFile zf = new ZipFile(ms);
            zf.UseZip64 = UseZip64.Off;
            zf.IsStreamOwner = false;
            ZipEntry entry = zf.GetEntry("content.xml");
            Stream s = zf.GetInputStream(entry);

            // Convert stream to string
            StreamReader reader = new StreamReader(s);
            string contentXml = reader.ReadToEnd();

            // Search for all links that start with "applnet.test.fr"
            string pattern = @"http://applnet\.test\.fr/GetContenu/Download\.aspx\?p1=.*?;p2=.*?;p5=.*?;p6=NOPUB";
            Regex regex = new Regex(pattern);
            MatchCollection matches = regex.Matches(contentXml);

            Directory.CreateDirectory(Path.GetDirectoryName(odtFilePath));

            // Process each link found
            foreach (Match match in matches)
            {
                string link = match.Value;
                string[] parts = link.Split(new string[] { "aspx?" }, StringSplitOptions.None);
                string queryString = parts[parts.Length - 1];


                // Download the corresponding intranet document
                string folderName = Path.GetFileNameWithoutExtension(odtFilePath);
                string subFolderName = "PJ - " + folderName;
                string fileName = queryString;
                string localFilePath = "C:/PiecesJointes/" + fileName;
                string onlineFilePath = "https://com.test.fr/files/test/test/" + queryString;

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
                request.AllowAutoRedirect = false;
                request.Method = "GET";
                request.ContentType = "application/pdf";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();
                byte[] buffer = new byte[4096];
                int bytesRead = 0;
                FileStream fileStream = new FileStream(localFilePath, FileMode.Create);

                do
                {
                    bytesRead = stream.Read(buffer, 0, buffer.Length);
                    fileStream.Write(buffer, 0, bytesRead);
                } while (bytesRead > 0);

                fileStream.Close();
                response.Close();


                // Replace the link with the path of the downloaded document
                string newLink = localFilePath.Replace("\\", "/");
                contentXml = contentXml.Replace(link, onlineFilePath);
            }

            // Updates the content.xml in the initial ZIP file
            byte[] contentXmlBytes = System.Text.Encoding.UTF8.GetBytes(contentXml);
            ms = new MemoryStream();
            zf.BeginUpdate();

            // Add updated content to ZIP file
            ZipOutputStream zos = new ZipOutputStream(ms);
            zos.UseZip64 = UseZip64.Off;
            zos.IsStreamOwner = false;

            // Add entry for content.xml file
            zos.PutNextEntry(new ZipEntry(entry.Name));
            StreamUtils.Copy(new MemoryStream(contentXmlBytes), zos, new byte[4096]);

            // Processes each entry from the original ODT file
            foreach (ZipEntry origEntry in zf)
            {
                // Ignore the entry for the content.xml file because it has already been added
                if (origEntry.Name == entry.Name) continue;

                // Add entry to new ZIP file
                zos.PutNextEntry(new ZipEntry(origEntry.Name));
                StreamUtils.Copy(zf.GetInputStream(origEntry), zos, new byte[4096]);
            }

            zos.Close();

            // Finish updating the ZIP file
            zf.CommitUpdate();
            zf.Close();


            // Renames and saves the updated ODT file
            Guid g = Guid.NewGuid();
            string updatedFilePath = Path.Combine(Path.GetDirectoryName(odtFilePath), g + "_" + Path.GetFileName(odtFilePath));
            using (FileStream stream = new FileStream(updatedFilePath, FileMode.Create))
            {
                ms.Position = 0;
                ms.WriteTo(stream);
            }
            Console.WriteLine("The ODT file has been successfully updated and saved as: " + updatedFilePath);
            Console.ReadLine();
        }
    }
}
 
Look closely at your line 51. It looks like you asked for a file name without file extension, and you use that too create your output file. Why are surprised that your files don't have a file extension?
 
Look closely at your line 51. It looks like you asked for a file name without file extension, and you use that too create your output file. Why are surprised that your files don't have a file extension?

how can I get the extension of the file downloaded ? because there is PDF files and docx files that can be downloaded
 
Part of the HTTP response should have the content type. You can then manually map content type to file extensions.
 
As a quick aside VS2010 hit end of life July 14, 2020.

That has no impact though an the code that you need to write to come up with the correct file extension for the corresponding content type. File extensions are a CPM/DOS/Windows OS thing. It has no real impact on the file contents. It does impact how Windows OSes treat files when you try to open them.
 
You're probably right. That sucks that the server would send 18KB of data just to tell you "page not found".
 
Indeed! IMG data urls have a lot to answer for :)

(Or, perhaps the url parameters cause it to run some query and prep a blank PDF for the queries that have no results.. I'm sure a screenshot of a hex ed on the file would allow us to explain it)
 
Hello, in these files i have a code for an identification page
1682320307278.png

Identification:
<!DOCTYPE HTML PUBLIC "-//W3C//Dtd HTML 4.01 Transitional//EN" >
<html>
<head>
    <title>INTRANET II - Identification centralisée</title>
    <meta content="no-cache" http-equiv="Pragma" />
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <meta name="description" content="INTRANET II - Powered by nginx & Mono" />
    <link rel="stylesheet" type="text/css" href="Login.css" />
    <script type="text/javascript">
        function StopClick(e) {
            if (!CheckFields())
                return true;

            e = e || window.event;
            var srcButton = e.srcElement;
            var login = document.getElementById("username").value;
            var pwd = document.getElementById("password").value;
            var btn = document.getElementById("btnValider");

            if ((login.length > 0) && (pwd.length > 0)) {
                try {
                    e.stopImmediatePropagation();
                } catch (e) { }
                try {
                    if (btn.AlreadySent) {
                        btn.disabled = true;
                        return true;
                    }
                    btn.AlreadySent = true;                   
                } catch (e) { }

            }
            return false;
        }       
   </script>
</head>
<body style="background-color:#F3F4F9;">
    <form name="Form1" method="post" action="login.aspx?service=http%3a%2f%2fapplnet.fiducial.fr%2fGetContenu%2fPageLogin.aspx%3fReturnUrl%3d%252fGetContenu%252fDownload.aspx%253fp1%253d1a4cd4a2-3987-4df1-be29-c0238cee790f%2526amp%253bp2%253d4%2526amp%253bp5%253d1%2526amp%253bp6%253dNOPUB" id="Form1">
<script type="text/javascript">//<![CDATA[
    var theForm;
    if (document.getElementById) { theForm = document.getElementById ('Form1'); }
    else { theForm = document.Form1; }
//]]></script>

<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="BHwoHXdYJgajcCF8FmODQz1qZpOBN5KoaqJWen8haCeSxSTYhB9K4zb1fQ5Pq45uJVUj7U2yRoBEenAMp+yz5ZClCJdPH0KhLcmlAHM/s8BSA2dlDen/9Mc2nUZ3WxMyV5r33Gau/u6um2NbKdIXBFfMNaV+s9scUuNPilqka6RW4OoM9aTMKyL2fqkHd3uqxNvFZrFDvl8tCV1YTlC9/Yrq3/7Hj88XDNAEuoCGYMsMDCIjK9/0yN/3Us87tM/ymQND1ETX3s/OJASLA3tM6LEipr2X/Se7bhqBVDG81EkTMMCLaNsxSruCxWvKlxyfXNBbmyl4n5xhIno9vJ0UN+rxwVZc47fgncpY4B0QIVFXdmEK12wTP8PEiy8t4c0YY4l1iffke8y8+G08BgTpl4TkaoqhpzWVc4phG+8rwLQUK72r8wjls4aNiEiqWxVm5rRB3W1KIHo1URIFpKKgHSv2jUtQk8ujKGCnSU6Bh+vX2XgOj70jf9UJoP55jPFeXN539YluZDweFi8u6Hw7GnwLNXQOp9/JwItaAnHCWZERXogG9V9/YmfwsUUUF2xZ3VyigtecHroJbL54sTgouGGEp9ghnbZE25aInWyefbP3SKFcMUmdDWvRm+W3WKYdezM9uM0xpJHtXafKmqM/Ko9dNSku3FEKz8HyqN0o+XQ949uj0x0WU5FzQ6zVY2XJL1OXLEgR1x6h0AyE344kvS3hPX+33PMnCHnZVgDOTvytClOEl3erqYewJnX8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NDPIMQNDpQuQe76ZalyCkNjflZwUT/zu0qGYnN/Whh161ZYdO86F5AdbPq+BlrBVU3fFwJ68NJYdjhdKv4u4i23K7aF+8ZxzSqIeh08BXv/QJVReKkjUPR0kTrqdU7HDcLUX+XGXPv2eGRuj9fpKpdHXuj6SCDBI6lagSE+p0By8JRN6708eYCRds7d6j+XcjHgM+LrOT4Utk+NetFaepDJI5GZq9SCbBenTUR6OAnlV7PptpjIg/vO4QW3Zt+xP2MV0z2kllaX1wHss1j3cItSykqfYAUwYE8wsEwwOrxkrv7C21Q5rtukLaPmUUHOjOz9F6Nyw2ZwzHb2Thw/fX5YhZ0/eMq+tJsZyBCfTef2qdbgJVhFELipwY8Oa7VXjkSFsXtaty1fjIlztLH182n2bhYfTf/8/vWQn7BSFqaNsqetQPnNoZwX1cok48nLI5aI6UOWa0VFFzsCqyP70f+myclzAo8ft2GI5NJQNDx5ILb+2EU04vV348/1dFQhDq2pMuPCZjqZo2PMwWAKAfk+OTlnROPKj7mbqYmGaSOQnbbdwLz5fIrrnRpbTehF1hpKHB/CEPLD7TInfKRVNOU9ZZXXyqsoJnNngAZdei4os4m8BJBhdYIdJdyS4S8AG/UktyJTEMls/QHL2obyN1Ngl1TUEar+DMs5KsV1S8YiVd7dpA=" />
</div>


                <script>               
                function CheckFields()
                {
                    var loginField=document.getElementById('username');
                    var passField=document.getElementById('password');
                    
                    var err1Login=document.getElementById('spanLogin');
                    var err2Login=document.getElementById('messLogin');
                
                    var err1Pass=document.getElementById('spanPass');
                    var err2Pass=document.getElementById('messPass');

                    var globalErrorMess=document.getElementById('lblError');

                    var error=false;
                    if (loginField.value.length==0)
                    {
                        error=true;
                        err1Login.style.visibility='visible';
                        err2Login.style.visibility='visible';
                        err2Login.style.display='';
                    }else
                    {
                        err1Login.style.visibility='hidden';
                        err2Login.style.visibility='hidden';
                    }
                    
                    if (passField.value.length==0)
                    {
                        error=true;
                        err1Pass.style.visibility='visible';
                        err2Pass.style.visibility='visible';
                        err2Pass.style.display='';
                    }else
                    {
                        err1Pass.style.visibility='hidden';
                        err2Pass.style.visibility='hidden';
                    }

                    if ((error)&&(globalErrorMess))
                    {
                        globalErrorMess.style.visibility='hidden';
                    }

                    return (error==false);
                }
                </script>
                

    <div id="pnlLogin" align="center">
    
        <center>
            <div id="pnlWarning">
        
                <table style="border-bottom: gray 1px solid; border-top: gray 1px solid; background-color:#FFFAF0; margin: 0 auto;" id="Table7" border="0" cellspacing="1" cellpadding="1" width="600">
                    <tr>
                        <td>
                            <span id="lblWarning1" class="Label" style="color:#C00000;">Pour des raisons de s&#233;curit&#233;, fermez votre navigateur lorsque vous avez termin&#233; d'utiliser les services requ&#233;rant une identification Fiducial Intranet.</span><br/>
                        </td>
                    </tr>
                </table>
                <br/>
                <br/>
            
    </div>
              <table id="Table1" border="0" cellspacing="0" cellpadding="0" style="background-color:#C0C0C0;">
                <tr>
                    <td>
                        <div id="Panel3" class="PanelOmbre" style="background-color:#F4F4F6;border-width:0px;width:300px;">
        
                            <table id="Table3" border="0" cellspacing="0" cellpadding="0" width="300">
                                <tr>
                                    <td style="height:113; background-color:#F3F4F9; background-image:url(Images/Logo.gif); border-left: #7386d2 1px solid; background-repeat: no-repeat; background-position: center center; border-top: #7386D2 1px solid; border-right: #7386D2 1px solid">
                                        <br/>
                                        </td>
                                </tr>
                            </table>
                            <table style="border-bottom: #7386d2 1px solid; border-left: #7386d2 1px solid; border-right: #7386d2 1px solid; background-color:#f4f4f6;"
                                id="Table5" border="0" cellspacing="0" cellpadding="0" width="300">
                                <tr>
                                    <td style="height:10; width:15;"></td>
                                    <td style="height:10;"></td>
                                    <td style="height:10;"></td>
                                    <td style="height:10; width:15;"></td>
                                </tr>
                                <tr>
                                    <td style="height:29; width:15;"></td>
                                    <td style="height:29;">
                                        <span id="lblLogin" class="Label">Identifiant</span></td>
                                    <td style="height:29; width:185;" align="right">
                                        <input type="text" name="username" id="username" class="TextBox" style="width:180px;" /></td>
                                    <td style="height:29; width:15;">
                                        <span id="spanLogin" class="Label" style="color:#C00000;visibility:hidden;">*</span>
                                        </td>
                                </tr>
                                <tr>
                                    <td style="height:29; width:15;"></td>
                                    <td style="height:29;">
                                        <span id="lblPassword" class="Label" style="display:inline-block;width:80px;">Mot de passe</span></td>
                                    <td style="height:29;" align="right">
                                        <input type="password" name="password" id="password" class="TextBox" style="width:180px;" /></td>
                                    <td style="height:29;">
                                        <span id="spanPass" class="Label" style="color:#C00000;visibility:hidden;">*</span>
                                        </td>
                                </tr>
                                <tr>
                                    <td style="height:29; width:15;"></td>
                                    <td style="height:29;">
                                        <span id="lblRemember" class="Label" style="display:inline-block;width:80px;">Se souvenir de l'identifiant</span></td>
                                    <td style="height:29;" align="left">
                                        <input id="cbxRemember" type="checkbox" name="cbxRemember" /></td>
                                    <td style="height:29;"></td>
                                </tr>
                                <tr>
                                    <td style="height:29; width:15;">
                                    </td>
                                    <td style="height:29;">
                                        <span id="lblUserLanguage" class="Label" style="display:inline-block;width:80px;">Langue</span>
                                    </td>
                                    <td style="height:29;" align="left">
                                        <select name="language" onchange="setTimeout('__doPostBack(\'language\',\'\')', 0)" id="language">
            <option value="default"></option>
            <option value="fr-FR">fr-FR</option>
            <option value="nl-BE">nl-BE</option>

        </select>                                           
                                    </td>
                                </tr>
                                <tr>
                                    <td style="width:15;"></td>
                                    <td colspan="2"></td>
                                    <td></td>
                                </tr>
                                <tr>
                                    <td style="width:15;"></td>
                                    <td valign="top" colspan="2" align="left">
                                        <table id="Table4" border="0" cellspacing="1" cellpadding="1" width="250">
                                            <tr>
                                                <td>
                                                    <span id="messLogin" class="Label" style="color:#C00000;display:none;">Identifiant manquant<br></span>
                                                    <span id="messPass" class="Label" style="color:#C00000;display:none;">Mot de passe manquant<br></span>                                                   
                                                    <br />
                                                    <br/>
                                                    <span id="lblCapsLock" class="Label" style="color:#C00000;"></span></td>
                                            </tr>
                                        </table>
                                    </td>
                                    <td></td>
                                </tr>
                                <tr>
                                    <td style="width:15;"></td>
                                    <td><img alt="" src="Images/fidusmall.gif" width="99" height="40"/></td>
                                    <td align="center">
                                        <input type="submit" name="btnValider" value="Valider" onclick="if (StopClick(event)){return false;};" id="btnValider" class="Button" style="border-style:Solid;width:99px;" /></td>
                                    <td></td>
                                </tr>
                                <tr>
                                    <td style="height:10; width:15;"></td>
                                    <td style="height:10"></td>
                                    <td style="height:10" align="right"></td>
                                    <td style="height:10"></td>
                                </tr>
                            </table>
                        
    </div>
                        </td>
                    <td valign="top" style="width:4;">
                        <img id="Image2" src="Images/coin.gif" style="border-width:0px;" /></td>
                </tr>
                <tr>
                    <td style="height:4">
                        <img id="Image1" src="Images/coin.gif" style="border-width:0px;" /></td>
                    <td style="height:4"></td>
                </tr>
            </table>
            <table id="Table2" border="0" cellspacing="0" cellpadding="0" width="200">
                <tr>
                    <td style="height:1" align="center">
                        <a id="lnkForgotPass" class="Lien" href="ForgotPassword.aspx">Mot de passe oubli&#233; ?</a>
                    </td>
                    <td style="height:1" align="center">
                        
                    </td>
                </tr>
                <tr>
                    <td style="height:1" colspan="2" align="center">
                        <a id="lnkHelp" class="Lien" href="./Help/Connection_Intranet_FR.pdf" target="_blank">Aide premi&#232;re connexion</a>
                    </td>
                </tr>
                <tr>
                    <td style="height:1;" colspan="2" align="center">
                        <a id="lnkTestConfig" class="Lien" href="TestConfig.aspx" target="_blank" style="z-index: 0">Version des applets</a>
                    </td>
                </tr>
                <tr>
                    <td style="height:1;" align="center">
                        
                    </td>
                    <td style="height:1" align="center">
                    </td>
                </tr>
            </table>
            <br/>
            </center>
    
</div>
    
    
        <input type="hidden" name="detectpost" value="1" />
    
<div>


<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="j2W6T/d0VJcdUIfBY9toTaLCQQR9JYqHcmfrX1HNTdHGOcoQKygTpV4EF48p6OR6zEs9UNdpuotXSq4CL1WzNun/Cfdnv8wifFc61RyhOaxidNuUp9bWZd+vaIYhN9RRdUZ/+ji56t30BOW5KRu/mx8dJJZ5vNYcvmpbxsNs02gtWwtLR//tjS2ykghAlJzeKFb8TNWovLfJEfiLj/jt6rdjbhAbABFRUy8ffOLY0RrGlPp+mWY6tT80Yr0TtqX1MVw5Fjb0GmsFMoHRIZ3owxwEVJ977ALvTBZSpx0+cF4h2sx1sFynLOaKcPNGYxTC6HzlqoYgqTPSqWLaW/uS5X6wEeWMWamxBSykPJmz81Q4ggvZ/9QlOcky1Dk8St8bf9Zk3VQxSRhyLGkzsqkPB8X5TA8=" /><input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /><input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
</div>

<script type="text/javascript">//<![CDATA[
window._form = theForm;
window.__doPostBack = function (eventTarget, eventArgument) {
    if(theForm.onsubmit && theForm.onsubmit() == false) return;
    theForm.__EVENTTARGET.value = eventTarget;
    theForm.__EVENTARGUMENT.value = eventArgument;
    theForm.submit();
}
//]]></script>

<script type='text/javascript' src='/s/cwcm/jscapslock.js'></script>
<script language='JavaScript'>try{document.getElementById('username').focus();}catch(e){}</script>
</form>

    <!-- Piwik -->
    <script type="text/javascript">
        var _paq = _paq || [];
        _paq.push(['trackPageView']);
        _paq.push(['enableLinkTracking']);
        (function () {
            var u = "/web/piwik/";
            _paq.push(['setTrackerUrl', u + 'piwik.php']);
            _paq.push(['setSiteId', 1]);
            var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0]; g.type = 'text/javascript';
            g.defer = true; g.async = true; g.src = u + 'piwik.js'; s.parentNode.insertBefore(g, s);
        })();
    </script>
    <noscript><p><img src="/web/piwik/piwik.php?idsite=1" style="border:0;" alt="" /></p></noscript>
    <!-- End Piwik Code -->

</body>
</html>
 
It makes sense. You will have to work out how a normal browser proves to the server that it is logged in; probably it submits a cookie or some kind of auth token along with the request for a page. If you submit the same cookie when you request a PDF it should download the file instead of this html

Open your web browser developer tools (usually F12) and download a file. Look at the request headers - probably there will be a cookie, or some kind of auth header that contains a long string of random looking characters. This is what you will have to replicate. It will change over time, so probably you will also have to build some code that calls to the login process and sends a username and password (which does not change), and the server will reply with the cookie/token you should use in future requests to prove that you logged in recently
 
I tried this, the program downloads 1 file (another 18kb with the identification code) and does not download the others and the timeout appears

C#:
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;

namespace PrepareDocForExternalUse
{
    class Program
    {

        static void Main(string[] args)
        {
            // Demande à l'utilisateur le chemin absolu d'un fichier ODT
            Console.WriteLine("Merci de renseigner le chemin absolu d'un fichier ODT:");
            string odtFilePath = Console.ReadLine();


            // Lis le contenu du fichier ODT
            byte[] content = File.ReadAllBytes(odtFilePath);
            MemoryStream ms = new MemoryStream();
            ms.Write(content, 0, content.Length);
            ZipFile zf = new ZipFile(ms);
            zf.UseZip64 = UseZip64.Off;
            zf.IsStreamOwner = false;
            ZipEntry entry = zf.GetEntry("content.xml");
            Stream s = zf.GetInputStream(entry);

            // Convertit le stream en string
            StreamReader reader = new StreamReader(s);
            string contentXml = reader.ReadToEnd();

            // Recherche tous les liens qui commencent par "applnet.fiducial.fr"
            string pattern = @"http://applnet\.fiducial\.fr/GetContenu/Download\.aspx\?p1=.*?;p2=.*?;p5=.*?;p6=NOPUB";
            Regex regex = new Regex(pattern);
            MatchCollection matches = regex.Matches(contentXml);


            Directory.CreateDirectory(Path.GetDirectoryName(odtFilePath));

            // Traite chaque lien trouvé
            foreach (Match match in matches)
            {
                string link = match.Value;
                string[] parts = link.Split(new string[] { "aspx?" }, StringSplitOptions.None);
                string queryString = parts[parts.Length - 1];



                // Télécharge le document intranet correspondant
                string folderName = Path.GetFileNameWithoutExtension(odtFilePath);
                string subFolderName = "PJ - " + folderName;
                string fileName = queryString;
                string localFilePath = "C:/PiecesJointes/" + fileName;
                string onlineFilePath = "https://com.fiducial.fr/files/fiducial/banque/" + queryString;

                string uriString = "http://applnet.fiducial.fr/GetContenu/PageLogin.aspx";


                string username = "username";
                string password = "password";

                CookieContainer cookieContainer = new CookieContainer();
                string postData = "username=" + username + "&password=" + password;
                byte[] postDataBytes = Encoding.UTF8.GetBytes(postData);

                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uriString);
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.ContentLength = postDataBytes.Length;
                httpWebRequest.CookieContainer = cookieContainer;

                Stream requestStream = httpWebRequest.GetRequestStream();
                requestStream.Write(postDataBytes, 0, postDataBytes.Length);
                requestStream.Close();

                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

                CookieCollection cookies = httpWebResponse.Cookies;


                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
                request.Timeout = 30000;
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(cookies);

                request.AllowAutoRedirect = true;
                request.Method = "GET";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                
                Stream stream = response.GetResponseStream();
                byte[] buffer = new byte[4096];
                int bytesRead = 0;
                FileStream fileStream = new FileStream(localFilePath, FileMode.Create);

                do
                {
                    bytesRead = stream.Read(buffer, 0, buffer.Length);
                    fileStream.Write(buffer, 0, bytesRead);
                } while (bytesRead > 0);


                fileStream.Close();
                response.Close();


                // Remplace le lien par le chemin du document téléchargé
                string newLink = localFilePath.Replace("\\", "/");
                contentXml = contentXml.Replace(link, onlineFilePath);
            }

            // Met à jour le content.xml dans le fichier ZIP initial
            byte[] contentXmlBytes = System.Text.Encoding.UTF8.GetBytes(contentXml);
            ms = new MemoryStream();
            zf.BeginUpdate();



            // Ajoute le contenu mis à jour au fichier ZIP
            ZipOutputStream zos = new ZipOutputStream(ms);
            zos.UseZip64 = UseZip64.Off;
            zos.IsStreamOwner = false;

            // Ajoute l'entrée pour le fichier content.xml
            zos.PutNextEntry(new ZipEntry(entry.Name));
            StreamUtils.Copy(new MemoryStream(contentXmlBytes), zos, new byte[4096]);


            // Traite chaque entrée du fichier ODT original
            foreach (ZipEntry origEntry in zf)
            {
                // Ignore l'entrée pour le fichier content.xml car il a déjà été ajouté
                if (origEntry.Name == entry.Name) continue;

                // Ajoute l'entrée au nouveau fichier ZIP
                zos.PutNextEntry(new ZipEntry(origEntry.Name));
                StreamUtils.Copy(zf.GetInputStream(origEntry), zos, new byte[4096]);
            }

            zos.Close();

            // Termine la mise à jour du fichier ZIP
            zf.CommitUpdate();
            zf.Close();


            // Renomme et enregistre le fichier ODT mis à jour
            Guid g = Guid.NewGuid();
            string updatedFilePath = Path.Combine(Path.GetDirectoryName(odtFilePath), g + "_" + Path.GetFileName(odtFilePath));
            using (FileStream stream = new FileStream(updatedFilePath, FileMode.Create))
            {
                ms.Position = 0;
                ms.WriteTo(stream);
            }
            Console.WriteLine("Le fichier ODT a été mis à jour avec succès et enregistré sous le nom : " + updatedFilePath);
            Console.ReadLine();
        }

    }
}
 
What are the values of link on line 85? On what values does the request time out on?

Not related to your problem, but lines 94-107 could simply be replaced with
C#:
using (var stream = response.GetResponseStream())
using (var fileStream = new FileStream(localFilePath, FileMode.Create))
{
    stream.CopyTo(fileStream);
}

From what I can see, you still aren't doing anything with regards to providing a file extension for the filename, but that was the issue with your original post, but your latest post seems to have encountered a different issue with regards to timing out.
 
- The value of link on line 85 is the string value of the match found by the regular expression pattern on line 47 (it's the files links that i have to download)
- I set the timeout to prevent the previous request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link); don't get stuck too long
- CopyTo method seems unavailable on my .NET version (3.5)
- Yes indeed, I did not take care of this problem because of the identification, I told myself that it would not be possible to recover the extension of the downloaded file if I cannot already download it
 
- The value of link on line 85 is the string value of the match found by the regular expression pattern on line 47 (it's the files links that i have to download)

Yes, I understand that. But since we don't have access to your initial "content.xml", we have no idea what the actual matches end up looking like. So please show us what those values end up being.
 
- I set the timeout to prevent the previous request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link); don't get stuck too long

Yes, that was pretty obvious. What URLs does it time out on? Hence the second question asking for what values of link does it time out on?
 
Back
Top Bottom