【发布时间】:2023-03-16 16:41:02
【问题描述】:
我正在尝试通过 Web 客户端访问安全 URL,但几乎没有错误。 总体目的是从安全 url 下载图像..
请注意,之前,我可以使用 http 从存储库下载图像,现在他们已在同一 url 中强制执行(安全)https。 我们有一个控制台应用程序,它定期运行并从服务器拉取图像并在本地存储。
请告知我做错了什么?还是缺少什么?
以下是我面临的问题。
- /// 如果我使用代码部分 1... 我收到错误消息说参数无效。
- /// 如果我使用代码部分 2... 文件保存到本地,但收到一条消息“文件似乎已损坏或过大且无法打开。
下面列出了第 1 节和第 2 节。
using (WebClient webClient = new WebClient())
{
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
webClient.Credentials = new NetworkCredential("id", "pwd"); // if credentials are wrong...still I get the imageByte
/// section 1 - if I use this code... I get error saying Parameter is not valid.
// ----------------------------- Start --------------------------------
imageByte = webClient.DownloadData("https://someurl/source/abcd.jpeg");
////Initialize image variable
Image newImage;
////Read image data into a memory stream
using (MemoryStream ms = new MemoryStream(imageByte, 0, imageByte.Length, true))
{
ms.Write(imageByte, 0, imageByte.Length);
//Set image variable value using memory stream.
newImage = Image.FromStream(ms, true, false); // Throws error at this line saying that parameter is not valid.
}
newImage.Save(@"location\image.jpeg");
// ----------------------------- End --------------------------------
/// section 2 - if I use this code... File gets saved to local but get a message " File appear to be corrupt or large and not able to open.
// ----------------------------- Start --------------------------------
webClient.DownloadFile("https://someurl/source/abcd.jpeg", @"location\image.jpeg");
// ----------------------------- End --------------------------------
}
【问题讨论】:
标签: c# webclient webclient-download