C# 从带有php头位置的URL获取文件名

C# 从带有php头位置的URL获取文件名,c#,C#,我有一个网站在php这段代码 switch($type){ case 'check': switch($action){ case 'update': echo "1.0.0.1"; break; } break; case 'download': $file = './ss/godzila.avi'; if (file_exis

我有一个网站在php这段代码

switch($type){
    case 'check':
        switch($action){
            case 'update':
                echo "1.0.0.1";
            break;
        }
    break;
    case 'download':
        $file = './ss/godzila.avi';
        if (file_exists($file)) {
            header("Location: {$file}");
            exit;
        }else{
            header("HTTP/1.0 404 Not Found");
        }
    break;
}
我是如何把c转换成文件名的?此文件仅用于测试。我将从更新服务器发送软件更新

我需要c中的文件名:

FileStream newFile = new FileStream(filePatch+fileName, FileMode.Create);
newFile.Write(downloadedData, 0, downloadedData.Length);

下载文件时,您只需检查HttpWebResponse.Headers,它们应该包含您发送的标题

我想你可以举一个例子,我根据你的具体用途对它做了一些修改nl:你需要得到的文件名

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace wget
{
    class Program
    {
        static bool DownloadFile(string url, string targetDirectory, out string realFilename, string defaultName = "unknown.txt")
        {
            // first set the filename to a non existing filename
            realFilename = string.Empty;
            bool succes = false;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = WebRequestMethods.Http.Get;
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    if (response.Headers.HasKeys())
                    {
                        /* in case no header found that is called "Location" you get your default set filename as a fallback */
                        realFilename = Path.Combine(targetDirectory, response.Headers["Location"] ?? defaultName);
                    }
                    else
                    {
                        realFilename = Path.Combine(targetDirectory, defaultName);
                    }
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        int blockSize = 8192;
                        byte[] buffer = new byte[blockSize];
                        int result;
                        if (!Directory.Exists(targetDirectory))
                        {
                            Directory.CreateDirectory(targetDirectory);
                        }
                        using (FileStream targetStream = new FileStream(realFilename, FileMode.Create, FileAccess.Write))
                        {
                            do
                            {
                                result = responseStream.Read(buffer, 0, buffer.Length);
                                targetStream.Write(buffer, 0, result);
                            } while (result > 0);
                        }
                    }
                }
                succes = true;
            }
            catch (WebException wex)
            {
                if (wex.Response != null)
                {
                    wex.Response.Close();
                }
                Console.WriteLine("WebException occured: {0}", wex.Message);
            }
            catch (FileNotFoundException fnfe)
            {
                Console.WriteLine("FileNotFoundException occured: {0} not found! {1}", fnfe.FileName, fnfe.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error occured: {0}", ex.Message);
            }

            return succes;
        }

        static void Main(string[] args)
        {
            string filename;
            if (DownloadFile("http://www.google.com", Environment.CurrentDirectory, out filename))
            {
                Console.WriteLine("Saved file to {0}", filename);
            }
            else
            {
                Console.WriteLine("Couldn't download file!");
            }
            Console.ReadLine();
        }
    }
}

你的意思是,你想从某个服务器下载文件?是的,我从许可证/更新服务器下载文件,文件名在标题中?这里是php代码。。标题位置…我下载文件,但需要文件名才能将filestream写入硬盘