When converting a HTML document from a given URL, the HTML converters will access the document using the GET HTTP method by default. The HiQPdf HTML to PDF and HTML to Image converters can also be configured to access the web page to convert using the POST HTTP method and to transmit a set of fields to the web server. For example, the HTTP POST fields to be transmitted to web server are added to the HttpPostFields collection of the HiQPdf.Chromium.HtmlToPdf object using the KeyValueCollectionAdd(String, String) method which takes as parameter the field name and the field value. If the collection is not empty the converter will make a POST request to the web page URL with the fields from this collection. If the POST fields collection is empty the converter will make a default GET request.
GET and POST Requests Demo
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using HiQPdf.Chromium;
namespace HiQPdf_Chromium_AspNetDemo.Controllers
{
public class GetAndPostController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ConvertToPdf(IFormCollection collection)
{
// Replace the demo serial number with the serial number received upon purchase
// to run the converter in licensed mode
Licensing.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";
// create the HTML to PDF converter
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// Set HTTP GET and POST parameters
string param1Name = collection["param1NameTextBox"][0].Length > 0 ? collection["param1NameTextBox"][0] : "param1";
string param1Value = collection["param1ValueTextBox"][0].Length > 0 ? collection["param1ValueTextBox"][0] : "Value 1";
string param2Name = collection["param2NameTextBox"][0].Length > 0 ? collection["param2NameTextBox"][0] : "param2";
string param2Value = collection["param2ValueTextBox"][0].Length > 0 ? collection["param2ValueTextBox"][0] : "Value 2";
string param3Name = collection["param3NameTextBox"][0].Length > 0 ? collection["param3NameTextBox"][0] : "param3";
string param3Value = collection["param3ValueTextBox"][0].Length > 0 ? collection["param3ValueTextBox"][0] : "Value 3";
string param4Name = collection["param4NameTextBox"][0].Length > 0 ? collection["param4NameTextBox"][0] : "param4";
string param4Value = collection["param4ValueTextBox"][0].Length > 0 ? collection["param4ValueTextBox"][0] : "Value 4";
string param5Name = collection["param5NameTextBox"][0].Length > 0 ? collection["param5NameTextBox"][0] : "param5";
string param5Value = collection["param5ValueTextBox"][0].Length > 0 ? collection["param5ValueTextBox"][0] : "Value 5";
string urlToConvert = collection["textBoxUrl"];
if (collection["HttpMethod"] == "postMethodRadioButton")
{
htmlToPdfConverter.HttpPostFields.Add(param1Name, param1Value);
htmlToPdfConverter.HttpPostFields.Add(param2Name, param2Value);
htmlToPdfConverter.HttpPostFields.Add(param3Name, param3Value);
htmlToPdfConverter.HttpPostFields.Add(param4Name, param4Value);
htmlToPdfConverter.HttpPostFields.Add(param5Name, param5Value);
}
else
{
Uri getMethodUri = new Uri(collection["textBoxUrl"]);
string query = getMethodUri.Query.Length > 0 ? "&" : "?" + String.Format("{0}={1}", param1Name, param1Value);
query += String.Format("&{0}={1}", param2Name, param2Value);
query += String.Format("&{0}={1}", param3Name, param3Value);
query += String.Format("&{0}={1}", param4Name, param4Value);
query += String.Format("&{0}={1}", param5Name, param5Value);
urlToConvert = collection["textBoxUrl"] + query;
}
byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(urlToConvert);
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "GET_And_POST_Request.pdf";
return fileResult;
}
}
}