【发布时间】:2017-12-28 15:46:15
【问题描述】:
我是 C# 表单的新手,最近尝试编写一个可以从我朋友的在线硬盘驱动器下载文件的程序。
但是我的程序只能正确下载小于 1mb 的文件,超过了只能下载 15bytes。
我做了一些似乎是maxRecievedMessageSize 问题的研究,我试图检查我的app.config,这就是我得到的
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
这是我的下载代码
MyHttpResponse httpDownload(String url)
{
string strHtml = string.Empty;
MyHttpResponse myResponse = new MyHttpResponse();
WebRequest myWebRequest = WebRequest.Create(url);
myWebRequest.Timeout = 10000;
HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
myHttpWebRequest.Timeout = 10000;
myHttpWebRequest.Method = "GET";
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Windows NT 5.2; Windows NT 6.0; Windows NT 6.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; MS-RTC LM 8; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 4.0C; .NET CLR 4.0E)";
myHttpWebRequest.CookieContainer = this.cookieContainer;
WebResponse myWebResponse = myHttpWebRequest.GetResponse();
Stream myStream = myWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myStream, Encoding.Default);
int index1 = url.LastIndexOf('/');
int index2 = url.LastIndexOf('?');
String FileName = url.Substring(index1 + 1, index2 - index1 - 1);
WebClient myWebClient = new WebClient();
//myWebClient.DownloadDataCompleted += new AsyncCompletedEventHandler(DownloadProgressCallback);
//trying to have this function but not sure how to code it right...
string filepath = textBox3.Text;
myWebClient.DownloadFileAsync(new Uri(url), filepath + "/" + FileName);
}
有人说DownloadFileAsync 可以“绝对成功下载文件,所以问题是我的代码或我的设置”
还有什么我可以提供的以提供更多信息吗?
我见过c# - maxReceivedMessageSize and maxBufferSize in app.config,但不太明白它在说什么。
顺便说一句,我不确定它是否与 WCF 有关。
【问题讨论】:
-
如果我只使用您显示的代码的最后 3 行,我可以从 here 下载任何大小的文件。看看你是否也可以。
-
我们在问题中不需要Thank you。默认情况下,我们假设您会心存感激。
-
非常抱歉,我在代码中做了一些白痴,修复后它工作正常,谢谢大家,非常抱歉
标签: c# winforms httprequest webclient