C# 我正试图从网站下载图像,但图像的url';s不工作为什么?
代码如下:C# 我正试图从网站下载图像,但图像的url';s不工作为什么?,c#,winforms,C#,Winforms,代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using unfreez_wrapper;
using Shell32;
namespace DownloadImages
{
public partial class Form1 : Form
{
string f;
string UrlsPath;
int counter;
UnFreezWrapper uf;
string localFilename;
public Form1()
{
InitializeComponent();
uf = new UnFreezWrapper();
counter = 0;
localFilename = @"d:\localpath\";
UrlsPath = @"d:\localpath\Urls\";
using (WebClient client = new WebClient())
{
client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=1&continent=europa#",localFilename + "test.html");
client.DownloadFile("http://www.sat24.com/en/eu?ir=true", localFilename + "test1.html");
}
f = File.ReadAllText(localFilename + "test1.html");
test("image2.ashx", "ir=true");
}
private void test(string firstTag, string lastTag)
{
List<string> imagesUrls = new List<string>();
int startIndex = 0;
int endIndex = 0;
int position = 0;
string startTag = firstTag;//"http://www.niederschlagsradar.de/images.aspx";
string endTag = lastTag;//"cultuur=en-GB&continent=europa";
startIndex = f.IndexOf(startTag);
while (startIndex > 0)
{
endIndex = f.IndexOf(endTag,startIndex);
if (endIndex == -1)
{
break;
}
string t = f.Substring(startIndex, endIndex - startIndex + endTag.Length);
imagesUrls.Add(t);
position = endIndex + endTag.Length;
startIndex = f.IndexOf(startTag,position);
}
string item = imagesUrls[imagesUrls.Count - 1];
imagesUrls.Remove(item);
for (int i = 0; i < imagesUrls.Count; i++)
{
using (WebClient client = new WebClient())
{
client.DownloadFile(imagesUrls[i], UrlsPath + "Image" + counter.ToString("D6"));
}
counter++;
}
List<string> files = Directory.GetFiles(UrlsPath).ToList();
uf.MakeGIF(files, localFilename + "weather", 80, true);
}
在列表中:imagesUrls我看到这9个URL:
例如,这在索引0中:image2.ashx?region=eu&time=201309162345&ir=true
我试过不用image2.ashx?
但在这两种情况下,我都会遇到一个错误:
client.DownloadFile(imagesUrls[i], UrlsPath + "Image" + counter.ToString("D6"));
论据例外
路径中的非法字符
在此之前,当我使用test.html和另外两个标记start和end时,它工作起来没有任何问题
但是现在我使用test1.html和这两个标签:test(“image2.ashx”,“ir=true”);
但是得到了例外
当我以一个图像url为例:image2.ashx?region=eu&time=201309170015&ir=true
并试图在chrome上冲浪,但没有收到任何结果,它试图在谷歌上搜索
它甚至不是一个url
这是完整的异常错误:
System.ArgumentException was unhandled
HResult=-2147024809
Message=Illegal characters in path.
Source=mscorlib
StackTrace:
at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
at System.Security.Permissions.FileIOPermission.CheckIllegalCharacters(String[] str)
at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
at System.IO.Path.GetFullPath(String path)
at System.Net.WebClient.GetUri(String path)
at System.Net.WebClient.DownloadFile(String address, String fileName)
at DownloadImages.Form1.test(String firstTag, String lastTag) in d:\C-Sharp\DownloadImages\DownloadImages\DownloadImages\Form1.cs:line 79
at DownloadImages.Form1..ctor() in d:\C-Sharp\DownloadImages\DownloadImages\DownloadImages\Form1.cs:line 45
at DownloadImages.Program.Main() in d:\C-Sharp\DownloadImages\DownloadImages\DownloadImages\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
那么我如何从这个url一个接一个地下载图片呢
当我使用test.html和startTag“:和endTag:“cultuur=en-GB&containment=europa”时
它工作得很好
但是现在使用test1.html和两个不同的标签是不起作用的。在异常点,imagesUrls[i]中到底是什么 你是说它类似于
image2.ashx?region=eu&time=201309162345&ir=true
如果是这样,您需要预先添加协议和服务器,即预先添加http://www.sat24.com/
给出http://www.sat24.com/image2.ashx?region=eu&time=201309162345&ir=true
但是,另一个问题是,您正在搜索image2.ashx
作为开始标记,然后搜索ir=true
作为结束标记。查看该页面的源代码,有许多image2.ashx
URI没有以ir=true
结尾
e、 g.http://www.sat24.com/image2.ashx?button=af260x160
当您在该URI中找到开始标记时,您将在找到结束标记之前获得大量HTML。问题在于图像url不是真实的url。因此,如何在该动画中找到每个图像url?
System.ArgumentException was unhandled
HResult=-2147024809
Message=Illegal characters in path.
Source=mscorlib
StackTrace:
at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
at System.Security.Permissions.FileIOPermission.CheckIllegalCharacters(String[] str)
at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
at System.IO.Path.GetFullPath(String path)
at System.Net.WebClient.GetUri(String path)
at System.Net.WebClient.DownloadFile(String address, String fileName)
at DownloadImages.Form1.test(String firstTag, String lastTag) in d:\C-Sharp\DownloadImages\DownloadImages\DownloadImages\Form1.cs:line 79
at DownloadImages.Form1..ctor() in d:\C-Sharp\DownloadImages\DownloadImages\DownloadImages\Form1.cs:line 45
at DownloadImages.Program.Main() in d:\C-Sharp\DownloadImages\DownloadImages\DownloadImages\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: