简体   繁体   English

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

[英]How Can i extract file name from a php download link

I am creating a program in c# using webbrowser control to view moodle of my university. 我正在使用Web浏览器控件在c#中创建一个程序,以查看我大学的情绪。 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 . 我下载使用公共类CookieAwareWebClient.class文件(发现这个代码在互联网上下载授权文件),但我不能得到正确的文件名或某些环节的例子- 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. 当我在常规的Chrome浏览器pdf文件中单击此文件时,“ http://somewebsite.com/pluginfile.php/186873/mod_resource/content/1/somefile.pdf ”打开。 but i cant het the name "somefile.pdf" .How i can get the original file name "somefile.pdf". 但我不能将名称命名为“ somefile.pdf”。如何获取原始文件名“ 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 我下面的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;
        }
    }
}

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. 因此,我已经使用webbrowser控件登录了网站,后来我使用了这些cookie来克服加密问题。

    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. 然后,我将响应发送到另一个函数,检查响应是文件还是URL,然后我将其下载。

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

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

相关问题 如何在C#中使用Sharpziplib从http下载和提取单个文件? - how can i download and extract single file from http using sharpziplib in c#? 如何从 pcap-ng 文件中的数据包数据中提取链路层数据? - How can I extract Link-Layer data from Packet Data in pcap-ng file? 下载链接php文件 - download link php file 如何通过 .net 控制台应用程序从 ftp 服务器下载名称中包含希伯来语字符的文件? - How can I download a file with Hebrew characters in its name from an ftp server via .net console application? 如何使用“正则表达式”提取文件名? - How can I use 'Regex' to extract a file name? 我如何使用C#代码从Google驱动器链接中知道原始文件名 - How can i know the original file name from google drive link using c# code 我可以用不同的名称保存查询字符串的下载链接吗? - Can I save the download link for the query string under a different name? 如何基于生成的.Net类或原始XSD中的元素/属性名称从xml文件中将值提取为字符串? - How can I extract values as strings from an xml file based on the element/property name in a generated .Net class or the original XSD? 如何下载网址中没有文件名和文件扩展名的文件 - How can i download a file without file name and file extension in the url 如何对下载的文件名进行编码? - How do I encode a file name for download?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM