解决Content-Disposition:中文文件名乱码attachment; filename=打鼾程度评估.pdf

下载文件获取Header文件名时,遇到中文乱码问题,通过以下代码处理解决:

                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                //发送请求并获取相应回应数据
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                string contentHeader = response.GetResponseHeader("Content-Disposition");
                string vs = contentHeader.Split('=')[1];

                Encoding iso = Encoding.GetEncoding("ISO-8859-1");
                Encoding utf8 = Encoding.UTF8;
                byte[] utfBytes = utf8.GetBytes(vs);
                byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
                string filename = utf8.GetString(isoBytes);

 

你可能感兴趣的:(c#,http)