WebRequestTimeout

HtmlLoadOptions.WebRequestTimeout property

The number of milliseconds to wait before the web request times out. The default value is 100000 milliseconds (100 seconds).

public int WebRequestTimeout { get; set; }

Remarks

The number of milliseconds that Aspose.Words waits for a response, when loading external resources (images, style sheets) linked in HTML and MHTML documents.

Examples

Shows how to set a time limit for web requests when loading a document with external resources linked by URLs.

public void WebRequestTimeout()
{
    // Create a new HtmlLoadOptions object and verify its timeout threshold for a web request.
    HtmlLoadOptions options = new HtmlLoadOptions();

    // When loading an Html document with resources externally linked by a web address URL,
    // Aspose.Words will abort web requests that fail to fetch the resources within this time limit, in milliseconds.
    Assert.AreEqual(100000, options.WebRequestTimeout);

    // Set a WarningCallback that will record all warnings that occur during loading.
    ListDocumentWarnings warningCallback = new ListDocumentWarnings();
    options.WarningCallback = warningCallback;

    // Load such a document and verify that a shape with image data has been created.
    // This linked image will require a web request to load, which will have to complete within our time limit.
    string html = $@"
        <html>
            <img src=""{ImageUrl}"" alt=""Aspose logo"" style=""width:400px;height:400px;"">
        </html>
    ";

    // Set an unreasonable timeout limit and try load the document again.
    options.WebRequestTimeout = 0;
    Document doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(html)), options);
    Assert.AreEqual(2, warningCallback.Warnings().Count);

    // A web request that fails to obtain an image within the time limit will still produce an image.
    // However, the image will be the red 'x' that commonly signifies missing images.
    Shape imageShape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
    Assert.AreEqual(924, imageShape.ImageData.ImageBytes.Length);

    // We can also configure a custom callback to pick up any warnings from timed out web requests.
    Assert.AreEqual(WarningSource.Html, warningCallback.Warnings()[0].Source);
    Assert.AreEqual(WarningType.DataLoss, warningCallback.Warnings()[0].WarningType);
    Assert.AreEqual($"Couldn't load a resource from \'{ImageUrl}\'.", warningCallback.Warnings()[0].Description);

    Assert.AreEqual(WarningSource.Html, warningCallback.Warnings()[1].Source);
    Assert.AreEqual(WarningType.DataLoss, warningCallback.Warnings()[1].WarningType);
    Assert.AreEqual("Image has been replaced with a placeholder.", warningCallback.Warnings()[1].Description);

    doc.Save(ArtifactsDir + "HtmlLoadOptions.WebRequestTimeout.docx");
}

/// <summary>
/// Stores all warnings that occur during a document loading operation in a List.
/// </summary>
private class ListDocumentWarnings : IWarningCallback
{
    public void Warning(WarningInfo info)
    {
        mWarnings.Add(info);
    }

    public List<WarningInfo> Warnings() { 
        return mWarnings;
    }

    private readonly List<WarningInfo> mWarnings = new List<WarningInfo>();
}

See Also