Using Aspose.Words for java with HTTPS V3

Hi Aspose Experts

We try to use Aspose service through secure communication (HTTPS V3), trying to convert HTML into a word document.

I’ll try to explain the problem: The reference to the images in the HTML is a URL to the file in our content server. Due to the HTTPS V3, the Aspose needs a certificate to the file address in our content server. The problem is that we don’t have a way to use Aspose with a certificate to load the file from the server (the function that replaces the file does not get a certificate as parameter and therefor it’s connection fails).

Is there a way to use the product with certificates to support this kind of work?

Thanks,
Sergey.

Hi Sergey,

Thanks for your inquiry. Unfortunately, there is no way to use Aspose.Words with certificate. However, since, Aspose.Words supports base64 as image source, you can create your own method to get images and replace image path in src attribute with base64 representation of the image. Here is a very simple code that demonstrates the technique. The code is in C# but I think it will not be difficult to implement the same in Java:

[Test]
public void Test001()
{
    // Get Html string.
    string html = File.ReadAllText(@"Test001\in.html");
    // Create a regular expression that will help us to find image SRCs.
    Regex urlRegex = new Regex("src\\s*=\\s*[\"']+(http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?)[\"']+");
    // Serch for SRCs.
    MatchCollection matchs = urlRegex.Matches(html);
    foreach (Match match in matchs)
    {
        // Replace urls with embedded base64 images.
        html = html.Replace(match.Groups[1].Value, GetBase64(match.Groups[1].Value));
    }
    // Now you can insert HTML into the document. All images are embedded into the HTML string.
    DocumentBuilder builder = new DocumentBuilder();
    builder.InsertHtml(html);
    builder.Document.Save(@"Test001\out.doc");
}

private string GetBase64(string imageUrl)
{
    string base64Data = "";
    try
    {
        // Prepare the web page we will be asking for
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageUrl);
        request.Method = "GET";
        request.ContentType = "image/jpeg";
        request.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";
        // Execute the request
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        //We will read data via the response stream
        Stream resStream = response.GetResponseStream();
        //Write content into the MemoryStream
        BinaryReader resReader = new BinaryReader(resStream);
        // Build base64 string.
        base64Data = string.Format("data:image/jpeg;base64,{0}",
        Convert.ToBase64String(resReader.ReadBytes((int)response.ContentLength)));
    }
    catch (Exception)
    {
    }
    return base64Data;
}

I hope such approach could help you to achieve what you need.

Best regards,