[英]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.