.net 通过Http范围请求快速检测GIF维度(在本地工作,在Azure上失败)
对于给定的URL(例如),下面的程序或多或少类似于在两个不同的环境中工作,但在Azure上失败,例外情况如下: System.ArgumentException:参数无效。在 System.Drawing.Image.FromStream(Stream-Stream,布尔值 使用EmbeddedColorManagement,Boolean validateImageData)在 System.Drawing.Image.FromStream(流) 在所有情况下,quetion中的程序集都是System.Drawing,版本=4.0.0.0,区域性=neutral,PublicKeyToken=b03f5f7f11d50a3a,因此我假设Azure上接收到的数据有所不同.net 通过Http范围请求快速检测GIF维度(在本地工作,在Azure上失败),.net,http,azure,.net,Http,Azure,对于给定的URL(例如),下面的程序或多或少类似于在两个不同的环境中工作,但在Azure上失败,例外情况如下: System.ArgumentException:参数无效。在 System.Drawing.Image.FromStream(Stream-Stream,布尔值 使用EmbeddedColorManagement,Boolean validateImageData)在 System.Drawing.Image.FromStream(流) 在所有情况下,quetion中的程序集都是Sy
public static DimensionResult Is404(string url)
{
DimensionResult result = null;
HttpWebRequest request = Http.PrepareGetRequest(new Uri(url), false, false, "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
request.Timeout = 2500;
request.Method = "GET";
request.AddRange(0, 2048);
request.KeepAlive = false;
request.AllowAutoRedirect = true;
try
{
result = new DimensionResult();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
result.ContentEncoding = response.ContentEncoding;
result.Url = response.ResponseUri.ToString();
result.Is404 = (response.StatusCode != HttpStatusCode.PartialContent && response.StatusCode != HttpStatusCode.OK) || System.Text.RegularExpressions.Regex.IsMatch(response.ContentType, "text|html", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if (!result.Is404)
using (System.Drawing.Image image = System.Drawing.Image.FromStream(response.GetResponseStream()))
{
result.Width = image.Width;
result.Height = image.Height;
}
}
}
catch (Exception ex)
{
result.Exception = ex;
result.Is404 = true;
}
return result;
}
请不要关注请求的字节数(这是一个简化版本),而是关注.NET网络堆栈中的哪些设置可以解释不同服务器之间的响应差异
在这两种情况下,到目前为止,我已经记录了响应头,它们是相同的,还没有网络跟踪:
日期:2012年4月20日星期五11:47:05
GMT,服务器:Apache,接受范围:字节,上次修改时间:2012年2月24日星期五
格林尼治时间17:21:00,内容范围:字节
0-2048/3556,内容长度:2049,内容类型:image/gif,缓存控制:max age=2592000,过期日期:Sun,
2012年5月20日格林尼治标准时间11:47:05,连接:关闭
更新:
我已经记录了在两个环境中接收到的字节,它们碰巧是相同的!所以相同的响应头、相同的响应长度、相同的响应内容、相同的程序集、不同的行为 根据,gif文件以字符“GIF87a”或“GIF89a”开头,后跟其宽度/高度。因此,如果您只需要宽度/高度信息,就可以读取字节6-9。不需要GDI+(System.Drawing.dll)。根据我的经验,在服务器环境中使用System.Drawing.dll,尤其是在Windows Azure中,可能会导致不可预知的结果。如果需要高级位图处理,可以使用WIC或WPF(在钩子下使用WIC)
致以最良好的祝愿
徐明