The application I am working on must take every link saved in an excel and run it, and in case the page status is 404, I have to save each link in a new excel (by that I mean in a excel to be all, not each link in a separate excel). So far I've managed to get every link to run, as well as create a new excel in case I manage to make it work, but unfortunately I'm stuck at this point where I don't know how or what to write in the code for save every time the status is 404 after checking the URLs in the existing Excel
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
using OfficeOpenXml;
namespace StartaproUrls
{
partial class Program
{
private static int statusCode;
private static HttpResponseMessage result;
private static object[,] valueArray;
static async Task Main(string[] args)
{
using var client = new HttpClient();
string path = @"C:\Users\stefanv\Downloads\startapro_urls.xlsx";
Application excelApp = new();
if (excelApp != null)
{
Workbook excelWorkbook = excelApp.Workbooks.Open(path);
Worksheet excelWorksheet = (Worksheet)excelWorkbook.Sheets[1];
Microsoft.Office.Interop.Excel.Range excelRange = excelWorksheet.UsedRange;
int rowCount = excelRange.Rows.Count;
int colCount = excelRange.Columns.Count;
valueArray = (object[,])excelRange.get_Value(
XlRangeValueDataType.xlRangeValueDefault);
for (int i = 1; i <= 20; i++)
{
for (int j = 1; j <= colCount; j++)
{
result = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, $"https://beta.startapro.hu{valueArray[i, j]}"));
statusCode = (int)result.StatusCode;
Console.WriteLine(statusCode); // or result
}
}
excelWorkbook.Close();
excelApp.Quit();
}
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
var file = new FileInfo(@"C:\Users\stefanv\Downloads\startapro_urlsError.xlsx");
var urlError = GetSetupData();
await CreateExcelForErrorUrls.SaveExcelFile(urlError, file);
}
public static List<UrlErrorModel> GetSetupData()
{
List<UrlErrorModel> output = new()
{
new() { UrlWithError = valueArray }
};
return output;
}
}
}
Bellow is the class for creating the Excel
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static StartaproUrls.Program;
namespace StartaproUrls
{
class CreateExcelForErrorUrls
{
public static async Task SaveExcelFile(List<UrlErrorModel> urlError, FileInfo file)
{
DeleteIFExists(file);
using var package = new ExcelPackage(file);
var ws = package.Workbook.Worksheets.Add("ErrorRaport");
var range = ws.Cells["A1"].LoadFromCollection(urlError, true);
range.AutoFitColumns();
await package.SaveAsync();
}
private static void DeleteIFExists(FileInfo file)
{
if (file.Exists)
{
file.Delete();
}
}
}
}
And Bellow is the Model for UrlError
namespace StartaproUrls
{
partial class Program
{
public class UrlErrorModel
{
public object UrlWithError { get; set; }
}
}
}