【问题标题】:Read ashx jpg with .Net 4.5使用 .Net 4.5 读取 ashx jpg
【发布时间】:2017-08-21 20:46:55
【问题描述】:

我的目标是从 ashx 网址读取 jpg 文件。我想用 Windows Phone 8 来做这件事,但我从 .Net 4.5 开始,因为这对我来说可能更简单。

这是一个示例网址: http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=239959&type=card

如果您在 IE 10 中访问此网址,您将看到一张图片。如何在 .Net 4.5 中下载图像?我试过使用:

HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/jpg"));
string resourceAddress = "http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=239959&type=card";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, resourceAddress);

HttpResponseMessage response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead);

byte[] responseBytes = await response.Content.ReadAsByteArrayAsync();

也使用 WebClient

string url = @"http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=220041&type=card";
byte[] imageData;
using (WebClient client = new WebClient())
{                
    imageData = client.DownloadData(new Uri(url));
}

这两种方法都不返回数据。如何获取数据并将其格式化为 jpg?我对使用 ashx 文件很陌生。我看到它们在 Asp.Net 网站中很容易使用,但找不到任何可以简单下载文件的东西。目标是下载 jpg 文件并将其显示在 windows phone 8 应用程序中。

【问题讨论】:

    标签: c# .net windows-phone .net-4.5 ashx


    【解决方案1】:

    看看那个网址。你已经对 & 进行了 URL 编码,所以确实没有返回任何内容

    http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=220041&type=card
    

    这个

    http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=220041&type=card
    

    但是,使用您的第一个代码示例在 Windows 8 中对我来说工作得很好。

    顺便说一句,我看到你设置了 image/jpg 的 Accept 标头,ASHX 似乎将 Content-type 设置为 image/jpeg - 它确实有效。

    【讨论】:

    • 做到了!谢谢你。我认为这会更困难。
    猜你喜欢
    • 2014-08-03
    • 2018-08-18
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多