[{"data":1,"prerenderedAt":70},["Reactive",2],{"/samples/csharp/httpwebrequest":3,"related:qK2QmaYT7L":60},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":11,"link":12,"body":13,"_type":55,"_id":56,"_source":57,"_file":58,"_extension":59},"/samples/csharp/httpwebrequest","csharp",false,"","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.","CSharp","HttpWebRequest","https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest?view=net-5.0",{"type":14,"children":15,"toc":52},"root",[16,31,43],{"type":17,"tag":18,"props":19,"children":20},"element","p",{},[21,29],{"type":17,"tag":22,"props":23,"children":26},"a",{"href":12,"rel":24},[25],"nofollow",[27],{"type":28,"value":11},"text",{"type":28,"value":30}," 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":17,"tag":32,"props":33,"children":37},"pre",{"className":34,"code":36,"language":5,"meta":7},[35],"language-csharp","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",[38],{"type":17,"tag":39,"props":40,"children":41},"code",{"__ignoreMap":7},[42],{"type":28,"value":36},{"type":17,"tag":32,"props":44,"children":47},{"className":45,"code":46,"language":5,"meta":7},[35],"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",[48],{"type":17,"tag":39,"props":49,"children":50},{"__ignoreMap":7},[51],{"type":28,"value":46},{"title":7,"searchDepth":53,"depth":53,"links":54},2,[],"markdown","content:samples:csharp:httpwebrequest.md","content","samples/csharp/httpwebrequest.md","md",[61,65,66],{"_path":62,"language":10,"library":63,"_id":64},"/samples/csharp/httpclient","HttpClient","content:samples:csharp:httpclient.md",{"_path":4,"language":10,"library":11,"_id":56},{"_path":67,"language":10,"library":68,"_id":69},"/samples/csharp/restsharp","RestSharp","content:samples:csharp:restsharp.md",1785426823582]