[{"data":1,"prerenderedAt":141},["Reactive",2],{"/samples/csharp":3},[4,61,101],{"_path":5,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":9,"description":10,"language":11,"library":12,"link":13,"body":14,"_type":56,"_id":57,"_source":58,"_file":59,"_extension":60},"/samples/csharp/httpclient","csharp",false,"","Httpclient","HttpClient is a class provided by the .NET framework that facilitates making HTTP requests and receiving HTTP responses. It offers a simple and flexible API for performing tasks such as sending GET, POST, PUT, and DELETE requests, handling headers and content, managing cookies, and configuring timeouts and other request parameters. HttpClient is widely used in C# for consuming web services, accessing RESTful APIs, and performing web scraping and automation tasks.","CSharp","HttpClient","https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-5.0",{"type":15,"children":16,"toc":53},"root",[17,32,44],{"type":18,"tag":19,"props":20,"children":21},"element","p",{},[22,30],{"type":18,"tag":23,"props":24,"children":27},"a",{"href":13,"rel":25},[26],"nofollow",[28],{"type":29,"value":12},"text",{"type":29,"value":31}," is a class provided by the .NET framework that facilitates making HTTP requests and receiving HTTP responses. It offers a simple and flexible API for performing tasks such as sending GET, POST, PUT, and DELETE requests, handling headers and content, managing cookies, and configuring timeouts and other request parameters. HttpClient is widely used in C# for consuming web services, accessing RESTful APIs, and performing web scraping and automation tasks.",{"type":18,"tag":33,"props":34,"children":38},"pre",{"className":35,"code":37,"language":6,"meta":8},[36],"language-csharp","using System;\nusing System.IO;\nusing System.Net.Http;\nusing System.Text.Json;\nusing System.Threading.Tasks;\n\npublic class PDFShiftConverter\n{\n    private readonly HttpClient _client;\n    private readonly string _apiKey;\n\n    public PDFShiftConverter(string apiKey)\n    {\n        _apiKey = apiKey;\n        _client = new HttpClient();\n    }\n\n    public async Task Convert(string endpoint, object parameters, string filePath)\n    {\n        if (!new[] { \"pdf\", \"png\", \"jpg\", \"webp\" }.Contains(endpoint))\n            throw new ArgumentException(\"Invalid endpoint\");\n\n        var request = new HttpRequestMessage(HttpMethod.Post, $\"https://api.pdfshift.io/v3/convert/{endpoint}\")\n        {\n            Content = new StringContent(JsonSerializer.Serialize(parameters), System.Text.Encoding.UTF8, \"application/json\")\n        };\n        request.Headers.Add(\"X-API-Key\", _apiKey);\n\n        var response = await _client.SendAsync(request);\n        response.EnsureSuccessStatusCode();\n\n        var jsonResponse = await response.Content.ReadAsStringAsync();\n\n        var dict = JsonSerializer.Deserialize\u003CDictionary\u003Cstring,object>>(jsonResponse);\n        if(dict.ContainsKey(\"filename\") || dict.ContainsKey(\"webhook\"))\n        {\n            return dict;\n        }\n\n        var binaryContent = await response.Content.ReadAsByteArrayAsync();\n\n        await File.WriteAllBytesAsync(filePath, binaryContent);\n    }\n}\n",[39],{"type":18,"tag":40,"props":41,"children":42},"code",{"__ignoreMap":8},[43],{"type":29,"value":37},{"type":18,"tag":33,"props":45,"children":48},{"className":46,"code":47,"language":6,"meta":8},[36],"new PDFShiftConverter(\"sk_XXXXXXXXXXXXXX\")\n    .Convert(\"pdf\", new { source = \"https://en.wikipedia.org/wiki/REST\" }, \"result.pdf\")\n    .GetAwaiter()\n    .GetResult();\n",[49],{"type":18,"tag":40,"props":50,"children":51},{"__ignoreMap":8},[52],{"type":29,"value":47},{"title":8,"searchDepth":54,"depth":54,"links":55},2,[],"markdown","content:samples:csharp:httpclient.md","content","samples/csharp/httpclient.md","md",{"_path":62,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":63,"description":64,"language":11,"library":65,"link":66,"body":67,"_type":56,"_id":99,"_source":58,"_file":100,"_extension":60},"/samples/csharp/httpwebrequest","Httpwebrequest","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.","HttpWebRequest","https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest?view=net-5.0",{"type":15,"children":68,"toc":97},[69,79,88],{"type":18,"tag":19,"props":70,"children":71},{},[72,77],{"type":18,"tag":23,"props":73,"children":75},{"href":66,"rel":74},[26],[76],{"type":29,"value":65},{"type":29,"value":78}," 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.",{"type":18,"tag":33,"props":80,"children":83},{"className":81,"code":82,"language":6,"meta":8},[36],"using System;\nusing System.IO;\nusing System.Net;\nusing System.Text;\n\npublic class ConversionAPIWrapper\n{\n    public static string Convert(string apiKey, string parameters, string endpoint = \"pdf\")\n    {\n        if (! (new[] {\"pdf\", \"png\", \"jpg\", \"webp\"}.Contains(endpoint))) \n        {\n            throw new ArgumentException(\"Invalid endpoint\");\n        }\n\n        string apiUrl = $\"https://api.pdfshift.io/v3/convert/{endpoint}\";\n        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(apiUrl));\n        request.Method = \"POST\";\n        request.Headers[\"X-API-Key\"] = apiKey;\n        request.ContentType = \"application/json\";\n        byte[] bytes = Encoding.ASCII.GetBytes(parameters);\n        using (Stream requestStream = request.GetRequestStream())\n        {\n            requestStream.Write(bytes, 0, bytes.Length);\n        }\n        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())\n        {\n            if (response.StatusCode == HttpStatusCode.OK)\n            {\n                using (StreamReader reader = new StreamReader(response.GetResponseStream()))\n                {\n                    return reader.ReadToEnd();\n                }\n            }\n            else\n            {\n                throw new Exception(\"API request failed: \" + response.StatusCode.ToString());\n            }\n        }\n    }\n}\n",[84],{"type":18,"tag":40,"props":85,"children":86},{"__ignoreMap":8},[87],{"type":29,"value":82},{"type":18,"tag":33,"props":89,"children":92},{"className":90,"code":91,"language":6,"meta":8},[36],"string paramsJson = File.ReadAllText(\"params.json\");  // Assuming parameters are stored inside a JSON file\nstring binaryResponse = ConversionAPIWrapper.Convert(\"sk_XXXXXXXXXXXXXX\", paramsJson);\nFile.WriteAllText(\"result.pdf\", binaryResponse);\n",[93],{"type":18,"tag":40,"props":94,"children":95},{"__ignoreMap":8},[96],{"type":29,"value":91},{"title":8,"searchDepth":54,"depth":54,"links":98},[],"content:samples:csharp:httpwebrequest.md","samples/csharp/httpwebrequest.md",{"_path":102,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":103,"description":104,"language":11,"library":105,"link":106,"body":107,"_type":56,"_id":139,"_source":58,"_file":140,"_extension":60},"/samples/csharp/restsharp","Restsharp","RestSharp is a popular third-party library that simplifies making HTTP requests and handling responses in .NET applications. It provides an intuitive and easy-to-use API for interacting with RESTful web services, enabling developers to perform common HTTP operations such as GET, POST, PUT, DELETE, and more. RestSharp abstracts away the complexities of HTTP communication, streamlining the process of consuming web APIs and processing their responses.","RestSharp","https://restsharp.dev/",{"type":15,"children":108,"toc":137},[109,119,128],{"type":18,"tag":19,"props":110,"children":111},{},[112,117],{"type":18,"tag":23,"props":113,"children":115},{"href":106,"rel":114},[26],[116],{"type":29,"value":105},{"type":29,"value":118}," is a popular third-party library that simplifies making HTTP requests and handling responses in .NET applications. It provides an intuitive and easy-to-use API for interacting with RESTful web services, enabling developers to perform common HTTP operations such as GET, POST, PUT, DELETE, and more. RestSharp abstracts away the complexities of HTTP communication, streamlining the process of consuming web APIs and processing their responses.",{"type":18,"tag":33,"props":120,"children":123},{"className":121,"code":122,"language":6,"meta":8},[36],"using RestSharp;\nusing System;\nusing System.IO;\nusing Newtonsoft.Json;\nusing RestSharp.Authenticators;\n\npublic class Conversion\n{\n    private RestClient Client;\n    string api_key = \"sk_XXXXXXXXXXXXXX\";\n\n    public Conversion(string api_key, string endpoint = \"pdf\") \n    {\n        if (! (new[] {\"pdf\", \"png\", \"jpg\", \"webp\"}.Contains(endpoint))) \n        {\n            throw new ArgumentException(\"Invalid endpoint\");\n        }\n\n        Client = new RestClient(\"https://api.pdfshift.io/v3/convert/\" + endpoint)\n        this.api_key = api_key;\n    }\n\n    public byte[] Convert(dynamic parameters) \n    {\n        RestRequest Request = new RestRequest(Method.POST);\n        Request.AddHeader(\"X-API-Key\", this.api_key);\n        Request.AddJsonBody(parameters);\n        var response = Client.Execute(Request);\n\n        if(response.IsSuccessful)\n        {\n            if (parameters.ContainsKey(\"filename\") || parameters.ContainsKey(\"webhook\")) \n            {\n                return System.Text.Encoding.Default.GetBytes(response.Content);\n            }\n            return response.RawBytes;\n        }\n        else\n        {\n            throw new Exception(response.ErrorMessage);\n        }\n    }\n}\n",[124],{"type":18,"tag":40,"props":125,"children":126},{"__ignoreMap":8},[127],{"type":29,"value":122},{"type":18,"tag":33,"props":129,"children":132},{"className":130,"code":131,"language":6,"meta":8},[36],"Conversion conversion = new Conversion(\"sk_XXXXXXXXXXXXXX\");\nvar json = new {\n    source = \"https://en.wikipedia.org/wiki/REST\"\n}\n\nbyte[] response = conversion.Convert(parameters);\nFile.WriteAllBytes(\"result.pdf\", response);\n",[133],{"type":18,"tag":40,"props":134,"children":135},{"__ignoreMap":8},[136],{"type":29,"value":131},{"title":8,"searchDepth":54,"depth":54,"links":138},[],"content:samples:csharp:restsharp.md","samples/csharp/restsharp.md",1785426823019]