我似乎無法找到我的問題的工作答案,我不知道是否有人可以幫助。基本上,我有我的網站的鏈接導致一個zip文件被下載:檢測到PHP鏈接導致在c#中下載文件
http://***.com/download.php?id=1
如果激活網頁上的這個鏈接就會彈出一個另存爲對話框,讓您保存默認文件名的文件ThisIsMyZipFile.zip
我的問題是,在C#如果我使用new Uri("http://***.com/download.pgp?id=1").IsFile
它false
所以我似乎返回不能檢測,這是不執行webclient DownloadString
,如果前兩個字節是PK
看到一個文件。
此外,即使手動下載爲字符串檢測到PK
標題並保存該文件我無法找到我的網站想要使用的默認文件名爲ThisIsMyZipFile.zip
在這個例子中,因爲我想使用相同的文件名。
有誰知道解決這兩個問題的好方法嗎?
UPDATE
感謝保羅和他的答案,我創建了下面的函數,它不正是我需要的:
/// <summary>
/// Returns the responded HTTP headers of the given URL and if the link refers to the file it returns extra information about it.
/// </summary>
/// <param name="Url">The address.</param>
/// <returns>
/// null if a WebException is thrown
/// otherwise:
/// List of headers:
/// Keep-Alive - Timeout value (i.e. timeout=2, max=100)
/// Connection - The type of connection (i.e. Keep-Alive)
/// Transfer-Encoding - The type of encoding used for the transfer (i.e. chunked)
/// Content-Type - The type of Content that will be transferred (i.e. application/zip)
/// Date - The servers date and time
/// Server - The server that is handling the request (i.e. Apache)
/// AbsoluteUri - The full Uri of the resulting link that will be followed.
/// The following key will be present if the link refers to a file
/// Filename - The filename (not path) of the file that will be downloaded if the link if followed.
/// </returns>
public Dictionary<string, string> GetHTTPResponseHeaders(string Url)
{
WebRequest WebRequestObject = HttpWebRequest.Create(Url);
WebResponse ResponseObject = null;
try
{
ResponseObject = WebRequestObject.GetResponse();
}
catch(WebException ex)
{
return null;
}
// Add the header inforamtion to the resulting list
Dictionary<string, string> HeaderList = new Dictionary<string, string>();
foreach (string HeaderKey in ResponseObject.Headers)
HeaderList.Add(HeaderKey, ResponseObject.Headers[HeaderKey]);
// Add the resolved Uri to the resulting list
HeaderList.Add("AbsoluteUri", ResponseObject.ResponseUri.AbsoluteUri);
// If this is a zip file then add the download filename specified by the server to the resulting list
if (ResponseObject.ContentType.ToLower() == "application/zip")
{
HeaderList.Add("Filename", ResponseObject.ResponseUri.Segments[ResponseObject.ResponseUri.Segments.Length-1]);
}
// We are now finished with our response object
ResponseObject.Close();
// Return the resulting list
return HeaderList;
}
MSDN說,ISFILE是,如果它匹配UriSchemeFile唯一的真。見http://bit.ly/TB6HZi和http://bit.ly/W6A2pA – MrSoundless