下载PDF文件

时间:2013-10-08 10:04:00

标签: c# path httpwebrequest filestream

如何在没有硬编码的情况下提供路径?

        HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create("http://lms.nsbm.lk/file.php/45/MIS_Notes/filename.pdf");

        getRequest.CookieContainer = new CookieContainer();
        getRequest.CookieContainer.Add(MoodleSessioncookie);
        getRequest.CookieContainer.Add(MoodleSessiontestcookie);
        getRequest.CookieContainer.Add(moodidcookie);

        HttpWebResponse form = (HttpWebResponse)getRequest.GetResponse();
        Stream responseReader = form.GetResponseStream();

        FileStream fs = File.Create("HardCoded Pdf File Name");
        byte[] data = new byte[512];
        int redcount = responseReader.Read(data, 0, data.Length);
        while (redcount > 0)
        {
            fs.Write(data, 0, redcount);
            redcount = responseReader.Read(data, 0, data.Length);
        }
        fs.Close();
        responseReader.Close();

0 个答案:

没有答案