FtpWebrequest - filename contains german “Umlaute” like ä,ö

你离开我真会死。 提交于 2019-12-02 03:25:21

问题


I'm trying to get a file via FTP per FtpWebrequest - the download fails when the filename contains german Umlaute like ä,ö,ü.

Code:

FtpWebRequest request2 = (FtpWebRequest)WebRequest.Create("ftp://re-web-03.servername.de/" + "filename with ä.xls");
request2.Method = WebRequestMethods.Ftp.DownloadFile;
request2.Credentials = new NetworkCredential("xxx", "xxx");
using (FtpWebResponse response = (FtpWebResponse)request2.GetResponse()) { // <-- Exception: The remote server returned an error: (550) File unavailable ...

When changing the file name to "filename with ae.xls" it works.

The Exception is: WebException: The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

A Directory Listing via ftp works well and shows the filename:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://re-web-03.servername.de/");
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential("xxx", "xxx");
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
    StreamReader sr = new StreamReader(response.GetResponseStream());
    while (!sr.EndOfStream)
    {   Console.WriteLine(sr.ReadLine()); } // --> output is "filename with ä.xls"
}

Output is "filename with ä.xls".

Does someone have a tip how to deal with that problem - I do not have any influence on naming that files ...

Many thanks in advance Tobi


回答1:


It might have something to do with encodings. Some OSes support utf-8 encoded filenames, others don't; if you send a request using utf-8 encoding, and the server interprets it as something else, it won't find the files you request. If, OTOH, you just request a directory listing, all goes well because utf-8 is backward compatible with ascii-7 (that is, valid ascii-7 is also valid utf-8). My guess is you're sending utf-8 and interpret the result as something else, or vv.




回答2:


Fetching the Filelist in UTF7 Encoding:

StreamReader sr = new StreamReader(response.GetResponseStream(),Encoding.UTF7);

did return the filename "Cases täglich .xls" in a way I can Downlod via Method "DownloadFile"



来源:https://stackoverflow.com/questions/3369863/ftpwebrequest-filename-contains-german-umlaute-like-%c3%a4-%c3%b6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!