Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Append a timestamp to the saved file name to avoid overwriting existing files.

using System;
using System.IO;
using System.Text;

class Program
{
static void Main()
{
try
{
string htmlContent = "<html><head><meta name=\"description\" content=\"old\"></meta></head><body><p>Hello World</p></body></html>";
byte[] bytes = Encoding.UTF8.GetBytes(htmlContent);
using (var inputStream = new MemoryStream(bytes))
using (var document = new Aspose.Html.HTMLDocument(inputStream, "about:blank"))
{
var metaElements = document.GetElementsByTagName("meta");
foreach (Aspose.Html.Dom.Element meta in metaElements)
{
if (meta.GetAttribute("name") == "description")
{
meta.SetAttribute("content", "new description");
}
}

string outputDir = Path.Combine(Directory.GetCurrentDirectory(), "Output");
Directory.CreateDirectory(outputDir);
string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmssfff");
string outputPath = Path.Combine(outputDir, $"result_{timestamp}.html");
document.Save(outputPath);
Console.WriteLine($"Saved to {outputPath}");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Implement asynchronous file download using Document.SendAsync and await the response before saving.

using System;
using System.IO;
using System.Threading.Tasks;
using Aspose.Html;
using Aspose.Html.Net;

class Program
{
static async Task Main(string[] args)
{
try
{
string url = "https://example.com/sample.bin";
string outputPath = "downloaded_file.bin";

using (HTMLDocument document = new HTMLDocument())
{
Url requestUrl = new Url(url);
RequestMessage request = new RequestMessage(requestUrl);

ResponseMessage response = await Task.Run(() => document.Context.Network.Send(request));

if (response.IsSuccess)
{
byte[] contentBytes = response.Content.ReadAsByteArray();
File.WriteAllBytes(outputPath, contentBytes);
Console.WriteLine($"File downloaded successfully to '{outputPath}'.");
}
else
{
Console.WriteLine($"Failed to download file. Status code: {response.StatusCode}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,35 @@

namespace Example
{
class AuthValidator : Aspose.Html.Net.MessageHandler
{
public override void Invoke(Aspose.Html.Net.INetworkOperationContext context)
{
Next(context);
string authHeader = context.Request.Headers["Authorization"];
if (!string.IsNullOrEmpty(authHeader))
System.Console.WriteLine("Authorization header present: " + authHeader);
else
System.Console.WriteLine("Authorization header missing");
}
}

class Program
{
static void Main(string[] args)
{
try
{
// Create configuration
var configuration = new Aspose.Html.Configuration();

// Get network service
var network = configuration.GetService<Aspose.Html.Services.INetworkService>();
Aspose.Html.Configuration configuration = new Aspose.Html.Configuration();

// Add a message handler to inspect Authorization header
Aspose.Html.Services.INetworkService network = configuration.GetService<Aspose.Html.Services.INetworkService>();
network.MessageHandlers.Add(new AuthValidator());

// Create request with basic authentication
var request = new Aspose.Html.Net.RequestMessage("https://example.com/protected");
Aspose.Html.Net.RequestMessage request = new Aspose.Html.Net.RequestMessage("https://example.com/protected");
request.Credentials = new System.Net.NetworkCredential("user", "pass");
request.PreAuthenticate = true;

// Load the HTML document using the authenticated request
using (var document = new Aspose.Html.HTMLDocument(request, configuration))
using (Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(request, configuration))
{
string html = document.DocumentElement != null ? document.DocumentElement.OuterHTML : string.Empty;
System.Console.WriteLine("Loaded HTML length: " + html.Length);
Expand All @@ -35,17 +42,4 @@ static void Main(string[] args)
}
}
}

public class AuthValidator : Aspose.Html.Net.MessageHandler
{
public override void Invoke(Aspose.Html.Net.INetworkOperationContext context)
{
Next(context);
string authValue = context.Request.Headers["Authorization"];
if (!string.IsNullOrEmpty(authValue))
System.Console.WriteLine("Authorization header present: " + authValue);
else
System.Console.WriteLine("Authorization header missing");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Configure a timeout for the RequestMessage to avoid hanging.

using System;

class Program
{
static void Main()
{
try
{
Aspose.Html.Net.RequestMessage request = new Aspose.Html.Net.RequestMessage("https://example.com");
request.Timeout = System.TimeSpan.FromSeconds(10);
using (Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(request))
{
string html = document.DocumentElement != null ? document.DocumentElement.OuterHTML : string.Empty;
System.Console.WriteLine(html);
}
}
catch (System.TimeoutException)
{
System.Console.WriteLine("The request timed out.");
}
catch (System.Exception ex)
{
System.Console.WriteLine("Error: " + ex.Message);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Create a console application that accepts a URL argument and saves the downloaded file to the current directory.

using System;
using System.IO;

class Program
{
static void Main(string[] args)
{
try
{
string urlString = args.Length > 0 ? args[0] : "https://example.com";
var url = new Aspose.Html.Url(urlString);
var document = new Aspose.Html.HTMLDocument();
var request = new Aspose.Html.Net.RequestMessage(url);
var response = document.Context.Network.Send(request);

if (!response.IsSuccess)
throw new Exception($"Failed to download. Status code: {response.StatusCode}");

byte[] contentBytes = response.Content.ReadAsByteArray();

string fileName;
try
{
var uri = new Uri(urlString);
fileName = Path.GetFileName(uri.LocalPath);
if (string.IsNullOrEmpty(fileName))
fileName = "index.html";
}
catch
{
fileName = "downloaded.html";
}

string outputPath = Path.Combine(Environment.CurrentDirectory, fileName);
File.WriteAllBytes(outputPath, contentBytes);

Console.WriteLine($"File saved to: {outputPath}");
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error: {ex.Message}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Create a FileStream for the destination path and copy the response stream to it.

using System;

public class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
{
public System.Collections.Generic.List<System.IO.MemoryStream> Streams = new System.Collections.Generic.List<System.IO.MemoryStream>();

public System.IO.Stream GetStream(string name, string extension)
{
var ms = new System.IO.MemoryStream();
Streams.Add(ms);
return ms;
}

public System.IO.Stream GetStream(string name, string extension, int page)
{
var ms = new System.IO.MemoryStream();
Streams.Add(ms);
return ms;
}

public void ReleaseStream(System.IO.Stream stream)
{
// No action needed
}

public void Dispose()
{
foreach (var s in Streams)
{
s.Dispose();
}
}
}

public class Program
{
public static void Main()
{
try
{
// Create a minimal HTML file
string htmlPath = "sample.html";
string htmlContent = "<html><body><h1>Hello Aspose.HTML</h1></body></html>";
System.IO.File.WriteAllText(htmlPath, htmlContent);

// Load the HTML document
var document = new Aspose.Html.HTMLDocument(htmlPath);

// Prepare provider and options
var provider = new MemoryStreamProvider();
var options = new Aspose.Html.Saving.PdfSaveOptions();

// Convert HTML to PDF using the provider
Aspose.Html.Converters.Converter.ConvertHTML(document, options, provider);

// Retrieve the generated stream
var resultStream = provider.Streams[0];
resultStream.Position = 0;

// Copy the result stream to a file
string outputPath = "output.pdf";
using (var fileStream = new System.IO.FileStream(outputPath, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
resultStream.CopyTo(fileStream);
}

Console.WriteLine("Conversion completed. PDF saved to " + outputPath);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ static void Main()
{
try
{
var targetUri = new System.Uri("https://www.example.com");
Console.WriteLine($"Target URL: {targetUri}");
var targetUri = new Uri("https://www.example.com");
Console.WriteLine("Created Uri: " + targetUri);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
Console.WriteLine("Error: " + ex.Message);
}
}
}
Loading
Loading