doudi2520 2019-02-18 18:36
浏览 223

我如何从php下载链接中提取文件名

I am creating a program in c# using webbrowser control to view moodle of my university. I am downloading files using public class CookieAwareWebClient.class (found this code in internet to download authorized files) but i cant get correct file name or some links example - http://somewebsite.com/mod/resource/view.php?id=80824 . When i click this in my regular chrome browser pdf file "http://somewebsite.com/pluginfile.php/186873/mod_resource/content/1/somefile.pdf" opens. but i cant het the name "somefile.pdf" .How i can get the original file name "somefile.pdf".

my function to download files

    public void saveFile(String url)
    {
        Uri Url = new Uri(@url);
        String filePath = "c:\\";
        fileName = url.Substring(url.LastIndexOf("/") + 1).Replace("%20", " ").Replace("%28", " ").Replace("%29", " ");
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.FileName = fileName;
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            filePath = saveFileDialog1.FileName;
            CookieAwareWebClient http = new CookieAwareWebClient(new CookieContainer());
            http.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
            http.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            string response = http.UploadString("http://courseweb.sliit.lk/login/index.php", "username=" + Form1.USERNAME + "&password=" + Form1.PASSWORD + "&submit=submit");
            //http.DownloadFile(Url, filePath);
            http.DownloadFileAsync(Url, filePath);
            this.Text = "Downloading File - " + fileName;
        }
    }

my CookieAwareWebClient.class below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace Course_Web
{
    public class CookieAwareWebClient : WebClient
    {
        Uri target = new Uri("http://unversitywebsite.com");
        public CookieContainer CookieContainer { get; set; }
        public Uri Uri { get; set; }



        public CookieAwareWebClient()
            : this(new CookieContainer())
        {
        }

        public CookieAwareWebClient(CookieContainer cookies)
        {
            this.CookieContainer = cookies;
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = this.CookieContainer;
            }
            HttpWebRequest httpRequest = (HttpWebRequest)request;
            httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            return httpRequest;
        }

        protected override WebResponse GetWebResponse(WebRequest request)
        {
            WebResponse response = base.GetWebResponse(request);
            String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];

            if (setCookieHeader != null)
            {
                //do something if needed to parse out the cookie.
                if (setCookieHeader != null)
                {
                    Cookie cookie = new Cookie("CookieName", "CookieValue") { Domain = target.Host }; //create cookie
                    this.CookieContainer.Add(cookie);
                }
            }
            return response;
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dsaeyrq451928 2019-02-24 03:03
    关注

    Finally I have found a solution

    I am posting it here because it may help anyone with same question. My university web site (moodle) is encrypted). So I have logged in to website with webbrowser control, Later I have used those cookies in order to overcome encryption problem.

        private String getURL(String url)
        {
            String response;
            CookieContainer jar;
            HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
            httpWebRequest.CookieContainer = jar;
            httpWebRequest.AllowAutoRedirect = false;
            httpWebRequest.Method = "HEAD";
            httpWebRequest.CookieContainer.SetCookies(URI,webBrowser1.Document.Cookie);
            HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
            response = httpWebResponse.GetResponseHeader("Location");
            if (response.Length == 0) response = url; //check for null String (if server din't response)
            httpWebRequest.Abort();
            return response;
        }
    

    Then, I have sent the response to another function checked the response is a file or url then i have downloaded it.

        private bool isfile(String url)
        {
            String tmpString = url.Substring(url.LastIndexOf("/") + 1);
            Boolean result = !(tmpString.Contains(".php") || tmpString.Contains(".htm") || tmpString.Contains(".asp") || tmpString.Contains(".cgi") || tmpString.Contains(".shtml") || tmpString.Contains(".jsp") || tmpString.Contains(".pl"));
            return result;
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?