C# 如何下载内容长度为0的C文件
我尝试了WebClient、HttpWebRequest、WebRequest和其他几种方法从特定服务器下载文件,但每次文件都是空的0字节。我发现在响应标题中:C# 如何下载内容长度为0的C文件,c#,download,httpwebrequest,webclient,content-length,C#,Download,Httpwebrequest,Webclient,Content Length,我尝试了WebClient、HttpWebRequest、WebRequest和其他几种方法从特定服务器下载文件,但每次文件都是空的0字节。我发现在响应标题中: Pragma: Public Content-Disposition: attachment; filename="hQPDAU0.mp3" Content-Transfer-Encoding: binary Connection: close Accept-Ranges: bytes Content-Length: 0 Cache-C
Pragma: Public
Content-Disposition: attachment; filename="hQPDAU0.mp3"
Content-Transfer-Encoding: binary
Connection: close
Accept-Ranges: bytes
Content-Length: 0
Cache-Control: max-age=1468800
Content-Type: audio/mpeg
Date: Tue, 08 Jul 2014 08:52:05 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Set-Cookie: sessioncode=4v0jgqiq.....1kulouk0c01; path=/; domain=.domain
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.4.29
该元素的长度为0。我已经在浏览器中打开了URL,它强制它下载文件。但是我怎样才能下载C语言的文件呢?将URL传递给WebMethod,希望它对您有用
[WebMethod]
public static string ProcessIT(string downloadURL, string file_name)
{
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
string path = @"c:\";
string path_n_name = path + file_name;
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(downloadURL, path_n_name);
return "SUCCESS";
}
检查用C获取和用浏览器获取请求之间的差异。例如,服务器可能正在查看用户代理…我将请求头设置为与在浏览器中相同的名称。我建议您使用Wireshark来确定两者的区别。我不知道如何使用Wireshark,我尝试过几次。我在Firefox中使用HttpFox插件,在Chrome和IE中使用开发者工具。现在使用Wireshark并不难——我建议你找一个好的教程。这是非常值得学习的。@r9s我不知道兄弟你是否测试过它,但我知道这肯定会帮助那些试图这样做的人。如果您已完成此项,请添加您自己的答案,谢谢。
public FileResult DownloadExcel(string filepath)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(filepath);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet,
`enter code here`Path.GetFileName(filepath));
}
------------------------------------------------------------------------