When converting an HTML document from a given URL, the HTML converters 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 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 the web server are added to the HttpPostFields collection of the HiQPdf.Next.HtmlToPdf object using the KeyValueCollectionAdd(String, String) method, which takes the field name and the field value as parameters. If the collection is not empty, the converter makes a POST request to the web page URL using the fields from this collection. If the POST fields collection is empty, the converter makes a default GET request.
By default the converter automatically applies application/x-www-form-urlencoded encoding to the names and values in the HttpPostFields collection when building the POST request body. If the names and values are already encoded by the caller, the automatic encoding can be disabled by setting the HtmlToPdfEncodeHttpPostFields property to false. The parameters transmitted in query string when using the GET method must still be URL-encoded by the caller.
GET and POST Requests Demo
using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using HiQPdf_Next_AspNetDemo.Models;
using HiQPdf.Next;
namespace HiQPdf_Next_AspNetDemo.Controllers
{
public class GetAndPostController : Controller
{
public IActionResult Index()
{
var model = new GetAndPostViewModel();
return View(model);
}
[HttpPost]
public ActionResult ConvertToPdf(GetAndPostViewModel model)
{
if (!ModelState.IsValid)
{
var errorMessage = ModelStateHelper.GetModelErrors(ModelState);
throw new ValidationException(errorMessage);
}
// Set the serial number received after purchase to use the library in licensed mode; leave it commented for demo mode
// Licensing.SerialNumber = "your-serial-number";
// Create an HTML to PDF converter object with default settings
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// Set HTTP GET and POST parameters
// The POST field names and values are automatically form-urlencoded by the converter
// Set the EncodeHttpPostFields property to false if they are already encoded by the caller
// The parameters transmitted in query string when using the GET method must be URL-encoded by the caller
string param1Name = !string.IsNullOrEmpty(model.Param1Name) ? model.Param1Name : "param1";
string param1Value = !string.IsNullOrEmpty(model.Param1Value) ? model.Param1Value : "Value1";
string param2Name = !string.IsNullOrEmpty(model.Param2Name) ? model.Param2Name : "param2";
string param2Value = !string.IsNullOrEmpty(model.Param2Value) ? model.Param2Value : "Value2";
string param3Name = !string.IsNullOrEmpty(model.Param3Name) ? model.Param3Name : "param3";
string param3Value = !string.IsNullOrEmpty(model.Param3Value) ? model.Param3Value : "Value3";
string param4Name = !string.IsNullOrEmpty(model.Param4Name) ? model.Param4Name : "param4";
string param4Value = !string.IsNullOrEmpty(model.Param4Value) ? model.Param4Value : "Value4";
string param5Name = !string.IsNullOrEmpty(model.Param5Name) ? model.Param5Name : "param5";
string param5Value = !string.IsNullOrEmpty(model.Param5Value) ? model.Param5Value : "Value5";
string urlToConvert = model.Url;
if (model.HttpMethod == "Post")
{
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(model.Url);
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 = model.Url + query;
}
byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(urlToConvert);
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "GET_And_POST_Request.pdf";
return fileResult;
}
}
}