我有以下代码从HTTPServer下载一组文件。它为前几个文件提供响应,然后在获取响应时陷入困境。我可以从我创建的日志文件中验证这一点。在日志文件中,它写入"在webresponse之前"但永远不会到达#34; webresponse"为第三个文件。我已经在使用webresponse了。什么可以在这里?
Logger.WriteToLog("url = " +url);
// Create a request to the file we are downloading
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Timeout = 120000;
webRequest.ReadWriteTimeout = 300000;
// Set default authentication for retrieving the file
//webRequest.Credentials = new NetworkCredential(GlobalVariables.username, GlobalVariables.password);
webRequest.UseDefaultCredentials = true;
// Retrieve the response from the server
Logger.WriteToLog("Before webresponse");
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
Logger.WriteToLog("After webresponse");
// Ask the server for the file size and store it
//Int64 fileSize = webResponse.ContentLength;
// Open the URL for download
using (Stream strResponse = webResponse.GetResponseStream())
{
// Create a new file stream where we will be saving the data (local drive)
strLocal = File.Create(destFilePath);
// Loop through the buffer until the buffer is empty
while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
if (isPaused)
waitRun_m.WaitOne();
if (isCanceled)
break;
strLocal.Write(downBuffer, 0, bytesSize);
// Invoke the method that updates the form's label and progress bar
UpdateProgessCallback(mediaName, bytesSize);
};
strResponse.Close();
}
}
答案 0 :(得分:0)
我尝试了ServicePoint类方法,现在工作正常。我做的是在为webrequest添加readwritetimeout之后,我使用以下语句为连接组分配了一个名称:
webRequest.ConnectionGroupName = some_name;
当关闭连接时:
webRequest.ServicePoint.CloseConnectionGroup(some_name);