我在使用WebClient类下载jpeg图像时遇到了一些问题。我试图通过以下代码下载jpeg图像:
public bool DownloadUri(string strUri, string destPath)
{
bool result = false;
try
{
Uri uri = new Uri(strUri, UriKind.RelativeOrAbsolute);
WebClient client = new WebClient();
client.DownloadFile(uri, destPath);
result = true;
}
catch (Exception ex)
{
result = false;
}
return result;
}
大多数工作正常,虽然当jpeg不存在时,我试图下载重定向的网站并且下载不会引发异常,它会下载一个只有8k大小的损坏的jpeg。这是我提供的一个令人讨厌的网址:
http://axisproperty.net.au/Upload/listing/500748870/500748870_1_500266205%2c1336368187%2cImageA.jpg
我已经使用Uri.TryCreate和Uri.IsWellFormedUriString(urlAttribute.Value,UriKind.Absolute)验证了uri。我试过做一个File.Exists(uri.ToString())来验证uri是否有一个与之关联的有效文件,尽管即使对于有效的url也是如此。
有没有其他方法来验证文件实际存在,所以我不下载所有这些不必要的损坏的jpeg文件?
答案 0 :(得分:0)
httpcl重定向不由webclient.downloadfile方法处理
你应该使用webrequest
public static int DownloadFile(String remoteFilename, String localFilename)
{
// Function will return the number of bytes processed
// to the caller. Initialize to 0 here.
int bytesProcessed = 0;
// Assign values to these objects here so that they can
// be referenced in the finally block
Stream remoteStream = null;
Stream localStream = null;
WebResponse response = null;
// Use a try/catch/finally block as both the WebRequest and Stream
// classes throw exceptions upon error
try
{
// Create a request for the specified remote file name
WebRequest request = WebRequest.Create(remoteFilename);
if (request != null)
{
// Send the request to the server and retrieve the
// WebResponse object
response = request.GetResponse();
if (response != null)
{
// Once the WebResponse object has been retrieved,
// get the stream object associated with the response's data
remoteStream = response.GetResponseStream();
// Create the local file
localStream = File.Create(localFilename);
// Allocate a 1k buffer
byte[] buffer = new byte[1024];
int bytesRead;
// Simple do/while loop to read from stream until
// no bytes are returned
do
{
// Read data (up to 1k) from the stream
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
// Write the data to the local file
localStream.Write(buffer, 0, bytesRead);
// Increment total bytes processed
bytesProcessed += bytesRead;
} while (bytesRead > 0);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
// Close the response and streams objects here
// to make sure they're closed even if an exception
// is thrown at some point
if (response != null) response.Close();
if (remoteStream != null) remoteStream.Close();
if (localStream != null) localStream.Close();
}
// Return total bytes processed to caller.
return bytesProcessed;
}
答案 1 :(得分:0)
对不起,我应该更清楚。我提供的网址无效,因此重定向到找不到网页或类似网页,因此我不想下载,因为该文件中没有有效的jpeg图片。我找到了以下解决方案:
public static bool CheckURL(string URL)
{
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
req.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
using (HttpWebResponse rsp = (HttpWebResponse)req.GetResponse())
{
if ((rsp.StatusCode == HttpStatusCode.OK) && rsp.ContentType.ToLower() == "image/jpeg")
{
return true;
}
}
}
catch
{
return false;
}
return false;
}