I have the following code. I'm doing some work on websites, but sometimes they redirect, and if they do so I want to do the work on the redirected site. BUT I don't want to check all the sites for redirection before doing the work, as there are few of them. So how can I insert it in the loop? I know that I can't insert into string array, but what's the best structure? Probably list, because of insert, although I'm not so keen to hold on to the values, which we have already processed. I'm not proficient in C#.
string[] lines = System.IO.File.Re开发者_StackOverflow社区adAllLines(@"my_file_with_urls.txt");
foreach (string line in lines)
{
Uri default_uri = new Uri(line);
Uri response;
WebSiteIsAvailable(default_uri, out response);
//Do work on the actual link
if (!response.Host.Equals(default_uri.Host)){
//I want to run the work on the redirected website
}
}
var urisToProcess = new HashSet<Uri>(
lines.Where(s => Uri.IsWellFormedUriString(s, UriKind.Absolute)).Select(s => new Uri(s)));
var redirectedUris = new HashSet<Uri>();
foreach (var uri in urisToProcess)
{
Uri response;
WebSiteIsAvailable(uri, out response);
if(response.Equals(uri))
{
// do work on actual URI
continue;
}
while (!response.Equals(uri))
{
uri = response;
WebSiteIsAvailable(uri, out response);
}
if(!urisToProcess.Contains(uri))
{
redirectedUris.Add(uri);
}
}
foreach (var uri in redirectedUris)
{
// do work on redirected URI
}
You could try, I havent fully tested it, but.
foreach (string line in lines)
{
Uri default_uri = new Uri(line);
Uri response;
WebSiteIsAvailable(default_uri, out response);
while (!response.Host.Equals(default_uri.Host))
{
default_uri = response;
WebSiteIsAvailable(default_uri, out response);
}
//Do work on the actual link as now default_uri contains the working link.
}
You can't insert items in the array or list you are going through with foreach. Instead make a for loop and decrease the index of the loop.
精彩评论