私は自分の問題に対する実用的な答えを見つけることができないようで、そこにいる誰かが助けてくれるのだろうかと思います。基本的に、私のWebサイトには、zipファイルをダウンロードするためのリンクがあります。
http://***.com/download.php?id=1
Webページでこのリンクをアクティブにすると、[名前を付けて保存]ダイアログが表示され、デフォルトの名前でファイルを保存できます。ThisIsMyZipFile.zip
私の問題は、c#を使用するnew Uri("http://***.com/download.pgp?id=1").IsFile
と返されるため、を実行して最初の2バイトがであるかどうかを確認しfalse
ないと、これがファイルであることを検出できないように見えることです。webclient DownloadString
PK
また、文字列として手動でダウンロードしてヘッダーを検出し、ファイルを保存しても、同じファイル名を使用したいので、この例PK
のデフォルトのファイル名としてWebサイトが使用したいものを見つけることができません。ThisIsMyZipFile.zip
誰かがこれらの2つの問題を解決する良い方法を知っていますか?
アップデート
Paulと彼の答えのおかげで、私は必要なことを正確に実行する次の関数を作成しました。
/// <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;
}