我没有得到任何例外,但大多数时候现在这一行:
if (response.ContentType == "")
大多数情况下,ContentType为空,它是“”
我的问题是为什么?如果有任何方法可以解决它? 我的程序每5分钟使用这种方法下载文件。
这就是我称之为方法的方法:
fileDownloadRadar(remote_image_on_server, combinedTemp);
remote_image_on_server是一个字符串,包含:http://www.ims.gov.il/Ims/Pages/RadarImage.aspx?Row=9&TotalImages=10&LangID=1&Location=
combinedTemp也是一个字符串:c:\ test \ test.gif
这是方法:
HttpWebRequest request;
int currentIndex = 0;
void fileDownloadRadar(string uri, string fileName)
{
if (splash != null)
{
if (!splash.IsDisposed)
splash.UpdateProgressBar(0);
}
try
{
request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
request.CookieContainer = new CookieContainer();
request.AllowAutoRedirect = true;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
long contentLength = response.ContentLength;
if (response.ContentType == "")
{
Logger.Write("ContentType is Empty download was not fine !!!!!");
}
if ((response.StatusCode == HttpStatusCode.OK ||
response.StatusCode == HttpStatusCode.Moved ||
response.StatusCode == HttpStatusCode.Redirect) &&
response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
{
Logger.Write("ContentType is not empty meaning download is fine");
using (Stream inputStream = response.GetResponseStream())
using (Stream outputStream = File.OpenWrite(fileName))
{
byte[] buffer = new byte[4096];
int bytesRead;
do
{
bytesRead = inputStream.Read(buffer, 0, buffer.Length);
currentIndex += bytesRead;
double percentage = (double)currentIndex / contentLength;
if (splash != null)
{
if (!splash.IsDisposed)
splash.UpdateProgressBar((int)(percentage * 100));
}
outputStream.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
if (splash != null)
{
if (!splash.IsDisposed)
{
splash.UpdateProgressBar(100);
}
}
}
}
else
{
timer1.Stop();
timer3.Start();
}
if (splash == null)
FinishWebRequest();
}
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.Timeout)
{
Logger.Write(ex.Status.ToString());
}
}
}
答案 0 :(得分:0)
这很可能是服务器的问题。如果你得到的东西不是200,那应该是你的起点。
如果您获得200,请尝试复制代码之外的行为。例如,使用插件自动刷新浏览器并观察响应。