View all our code samples

Using HttpWebRequest in CSharp

HttpWebRequest is a class provided by the .NET framework that allows developers to create and send HTTP requests to a web server and retrieve the corresponding HTTP responses. It provides a lower-level API compared to HttpClient, allowing for more fine-grained control over HTTP request parameters and headers. HttpWebRequest is suitable for scenarios where customizations beyond the capabilities of HttpClient are required, such as working with non-standard protocols or handling specific authentication methods.

We've implemented a code sample that you can re-use to convert your HTML documents to PDF, JPG, PNG or WEBP using PDFShift and CSharp:

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

public class ConversionAPIWrapper
{
    public static string Convert(string apiKey, string parameters, string endpoint = "pdf")
    {
        if (! (new[] {"pdf", "png", "jpg", "webp"}.Contains(endpoint))) 
        {
            throw new ArgumentException("Invalid endpoint");
        }

        string apiUrl = $"https://api.pdfshift.io/v3/convert/{endpoint}";
        string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("api:" + apiKey));
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(apiUrl));
        request.Method = "POST";
        request.Headers["Authorization"] = "Basic " + credentials;
        request.ContentType = "application/json";
        byte[] bytes = Encoding.ASCII.GetBytes(parameters);
        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(bytes, 0, bytes.Length);
        }
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            if (response.StatusCode == HttpStatusCode.OK)
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    return reader.ReadToEnd();
                }
            }
            else
            {
                throw new Exception("API request failed: " + response.StatusCode.ToString());
            }
        }
    }
}

Here's how you can use the above code:

string paramsJson = File.ReadAllText("params.json");  // Assuming parameters are stored inside a JSON file
string binaryResponse = ConversionAPIWrapper.Convert("sk_XXXXXXXXXXXXXX", paramsJson);
File.WriteAllText("result.pdf", binaryResponse);

We've tested this code with the latest version of HttpWebRequest and it's ready to be used in your project.

But if you were to encounter any bugs or issues while running it (or if you want to suggest changes to improve the code), please contact us and we'll be happy to help you out.