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

时间:2019-02-18 18:36:54

标签: c# php web controls

我正在使用Web浏览器控件在c#中创建一个程序,以查看我大学的心情。我正在使用公共类CookieAwareWebClient.class(在Internet上找到此代码以下载授权文件)下载文件,但是我无法获得正确的文件名或某些链接示例-http://somewebsite.com/mod/resource/view.php?id=80824。当我在常规的Chrome浏览器中点击此pdf文件时,“ http://somewebsite.com/pluginfile.php/186873/mod_resource/content/1/somefile.pdf”打开。但是我不能将其命名为“ somefile.pdf”。如何获取原始文件名“ somefile.pdf”。

我下载文件的功能

    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;
        }
    }

下面的我的CookieAwareWebClient.class

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 个答案:

答案 0 :(得分:0)

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;
    }