开发者

How can i get a file name from Response.Header ? C#

开发者 https://www.devze.com 2023-04-02 16:33 出处:网络
This is a code,C#. System.Net.HttpWebRequest _Response = (HttpWebRequest)System.Net.WebRequest.Create(e.Uri.AbsoluteUri.ToString());

This is a code, C#.

System.Net.HttpWebRequest _Response =
    (HttpWebRequest)System.Net.WebRequest.Create(e.Uri.AbsoluteUri.ToString());
_Response.Method = "GET";
_Response.Timeout = 120000;
_Response.Accept =
    "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
_Response.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
_Response.Headers.Add("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4");
_Response.Headers.Add("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.3");
_Response.AllowAutoRedirect = false;

System.Net.HttpWebResponse result = (HttpWebResponse)_Response.GetResponse();

for (int i = 0; i < result.Headers.Count; i++)
{
    MessageBox.Show(result.Headers开发者_开发技巧.ToString());
}

And this is a result,

Cache-Control: private
Content-Type: text/html
Date: Tue, 06 Sep 2011 17:38:26 GMT
ETag: 
Location: http://fs31.filehippo.com/6428/59e79d1f80a74ead98bb04517e26b730/Firefox Setup 7.0b3.exe
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET


The correct approach is to see if a filename is provided by the Content-Disposition field and, failing that, to attempt to infer a filename from the Location field.

Be aware than the location field is simply the URL for the download request, and as such may not include an extension or even a meaningful name.


Do it like this:

    string fileName = Path.GetFileName(result.Headers["Location"]);

That way, you'll have the file name at the end of the location header.


Given your headers from your request, you should be able to do:

 string file = result.Headers["Location"];


If you have got the location of the file you can just extract the header you want (in this case I suppose it is indexed at 4 or at "Location") and then take the last part of the URL.


The easy and efficient way to retrieve filename from Content-Disposition field:

using System.Net.Mime;

HttpWebResponse resp = {YOUR RESPONSE}
string dispHeader = resp.GetResponseHeader("content-disposition");
ContentDisposition disp = new ContentDisposition(dispHeader);
string filename = disp.FileName;


As the file is on a server you will not be able to retrieve the actual filename. Only what the web application is telling you.

This filename is in "Location".

However since the application is telling you that it is text/html it may be formatting the result before it sends it to you. The correct mime type for an executable is application/octet-stream.

On another note. It appears you are downloading the file in which case there is no need to be provided a path. The path of the file you download, is going to be whatever path you place the contents of the downloaded stream into. Therefore you save the file and put it wherever you have access to put it.

When the file is created you have to provide a path, otherwise it is placed in the same directory as the executable that is calling it.

I hope this helps


If all else fails, you can always parse the WebResponse.ResponseUri.ToString(). Use string.LastIndexOf("/") to find the beginning of the file name and String.IndexOf to see if there is a "?".

public static void ExtractFileNameFromUri(string URI, ref string parsedFileName, string fileNameStartDelimiter = "/", string fileNameEndDelimiter = "?")
{
    const int NOTFOUND = -1;

    try
    {
        int startParse = URI.LastIndexOf(fileNameStartDelimiter) + fileNameStartDelimiter.Length;

        if (startParse == NOTFOUND)
            return;

        int endParse = URI.IndexOf(fileNameEndDelimiter);

        if (endParse == NOTFOUND)
            endParse = URI.Length;

        parsedFileName = URI.Substring(startParse, (endParse - startParse));
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        return;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号