對於每一位 .NET 開發人員來說,這樣的情境再熟悉不過:您正在閱讀一個新 API 的說明文件,而提供者給了您一個 curl 指令來測試一個端點。 您盯著命令列工具的語法,嘆了一聲氣,然後開始將其翻譯成 C# 語言的新 HttpClient 範例的繁瑣過程。
您必須手動映射標頭、確保正文的字串值編碼正確、處理使用者代理程式,並希望您沒有遺漏默認預設值。 這種手動的 bash 複製和翻譯過程很容易出錯。 少了一個標頭,您的 HTTP 請求就會失敗。
輸入 CurlDotNet。 由 Iron Software 的 CTO Jacob Mellor 所創造,這個 .NET 函式庫完全改變了工作流程。 它允許您直接將 curl 指令貼到程式碼中,並以與終端機相同的行為執行。
什麼是 Curl DotNet? [CurlDotNet](https://github.com/jacob-mellor/curl-dot-net) 是 curl CLI 工具的純 .NET 實作。 與其他包裝程式不同,此程式庫沒有本機相依性 (如 libcurl.dll)。 本軟體完全以管理程式碼建立,這表示本軟體可在 Windows、Linux 及 macOS 上無縫執行,無須複雜的設定。 無論您是在 .NET Core 中開發 web 應用程式、主控台應用程式或 CI 管道,CurlDotNet 都能為 .NET 運行時帶來真正的 curl 語義。 ### 主要功能 * **零翻譯:**直接將 curl HTTPS 指令貼到 C# 原始碼中。 * **跨平台:**完全支援 Windows、Linux、MacOS 等作業系統。 * **類型安全:**可選擇使用流暢的建構器來處理依賴注入情境。 * **可觀察性:**內建支援發出結構化事件以記錄日誌。 ## 入門:安裝與執行 首先,您需要透過套件管理員安裝套件 curldotnet。 您可以在發佈筆記中找到最新版本,或直接執行: ``` dotnet 新增套件 CurlDotNet ``` 安裝完成後,即可立即執行 curl 呼叫。 ### 字串 API:粘貼和 Go Curl 指令 此方法非常適合健康檢查、支援代理或快速原型設計。 您只需將 curl 指令以字串形式傳送即可。 ```cs using CurlDotNet; // Simply paste the command string var response = await Curl.ExecuteAsync("curl https://api.github.com/users/octocat"); // Access the data Console.WriteLine(response.Body); ``` ### 利用環境變數使用 Fluent 建立程式。 對於需要透過環境變數處理敏感資料,或需要更簡潔架構的生產應用程式,fluent builder 是理想的選擇。 您可以為標頭指定字串名稱或明確設定檔案路徑。 ```cs var response = await Curl.GetAsync("https://api.example.com/data") .WithHeader("Authorization", "Bearer " + Environment.GetEnvironmentVariable("API_KEY")) .WithTimeout(TimeSpan.FromSeconds(30)) .ExecuteAsync(); ``` ## Iron Software Connection:真實世界的整合 [CurlDotNet](https://dev.to/iron-software/curldotnet-bringing-curl-superpowers-to-every-corner-of-the-net-10-c-stack-1ol2)由 Iron Software 贊助,該公司致力於建立工具,為開發人員解決最棘手的問題。 Jacob Mellor 創建 CurlDotNet 的理念與 Iron Software Suite 的理念相同:開發者的經驗是第一位的。 當與 Iron Software 產品結合時,CurlDotNet 的真正威力將會釋放出來。 您可以使用 Curl 來處理複雜的傳輸層(處理代理、傳統 auth 或特定的 curl http 怪癖),並使用 Iron 函式庫來處理文件的繁重工作。 ### 範例 1:使用 IronPDF 安全下載和編輯 PDF ! [IronPDF](/static-assets/ironsoftware/suite/blog/curl-dotnet/curl-dotnet-1.webp) [**IronPDF**](https://ironpdf.com) 是在 .NET 中生成像素完美的 PDF 的行業標準。 IronPDF [完全按照 Chrome 瀏覽器的方式呈現 HTML](https://ironpdf.com/tutorials/html-to-pdf)、[CSS](https://ironpdf.com/how-to/html-to-pdf-responsive-css)和 [JavaScript](https://ironpdf.com/how-to/javascript-to-pdf),與其他在現代網路標準上掙扎的函式庫不同。 IronPDF 是一套完整的解決方案:您可以從 [HTML 字串](https://ironpdf.com/how-to/html-string-to-pdf)或 [檔案](https://ironpdf.com/how-to/html-file-to-pdf)產生新文件、[編輯現有的 PDF](https://ironpdf.com/tutorials/csharp-edit-pdf-complete-tutorial)、合併文件,以及套用 [水印和加密等安全功能](https://ironpdf.com/tutorials/csharp-pdf-security-complete-tutorial),而不需要外部相依性或在伺服器上安裝 Adobe Acrobat。 試想一下,您需要從傳統的內部系統下載生成的發票,該系統需要複雜的 curl 標誌(例如忽略 SSL 錯誤或特定的標頭排列),然後用 IronPDF [水印](https://ironpdf.com)它。 將該請求翻譯為 HttpClient 可能需要數小時的調試時間。 使用 CurlDotNet,您可以貼上指令、取得位元組,然後將它們交給 [IronPdf](https://ironpdf.com)。 ```cs using CurlDotNet; using IronPdf; // 1. Use CurlDotNet to handle the complex transport // We use -k to allow insecure SSL (common in legacy internal apps) var curlCommand = "curl -k https://internal-billing.local/invoice/1234 -H 'X-Dept: Sales'"; var response = await Curl.ExecuteAsync(curlCommand); if (response.IsSuccess) { // 2. Pass the raw bytes directly to IronPDF // IronPDF renders the PDF from the downloaded data var pdfDocument = PdfDocument.FromPdf(response.BodyBytes); // 3. Apply a watermark and save pdfDocument.ApplyWatermark("CONFIDENTIAL", 30, VerticalAlignment.Middle, HorizontalAlignment.Center); pdfDocument.SaveAs("Processed_Invoice.pdf"); Console.WriteLine("Invoice downloaded and secured via IronPDF."); } ``` ### 控制台確認  ### PDF輸出  ### 範例 2:使用 IronOCR 進行掃描和 OCR ! [IronOCR](/static-assets/ironsoftware/suite/blog/curl-dotnet/curl-dotnet-4.webp) [**IronOCR**](https://ironsoftware.com/csharp/ocr) 是由 [Tesseract 5 引擎](https://ironsoftware.com/csharp/ocr/tutorials/c-sharp-tesseract-ocr)提供動力的先進光學字元識別庫,專為 C# 和 .NET 微調。 它支援超過[127種語言](https://ironsoftware.com/csharp/ocr/languages),並擅長從[不完美的來源](https://ironsoftware.com/csharp/ocr/how-to/image-quality-correction)讀取文字,例如低解析度掃描、旋轉的影像或嘈雜的背景。 它的 "電腦視覺 "功能可讓它自動偵測文字區域,不僅能以純字串形式輸出資料,還能以結構化內容(Barcode、段落、行和字元)形式輸出資料,以便進行深入分析。 有時候您需要 [從封鎖標準 .NET scraers 的伺服器上託管的影像](https://ironsoftware.com/csharp/ocr/tutorials/how-to-read-text-from-an-image-in-csharp-net)中抽取資料。 您可以使用 CurlDotNet 毫不費力地模仿標準的瀏覽器使用者代理,然後再使用 IronOCR 來閱讀文字。 ```cs using CurlDotNet; using IronOcr; // 1. Fetch the image using a specific browser User-Agent to bypass blocks var imgResponse = await Curl.GetAsync("https://site.com/protected-captcha.png") .WithUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64)") .ExecuteAsync(); // 2. Use IronOCR to read text from the file bytes var ocr = new IronTesseract(); using (var input = new OcrInput()) { input.AddImage(imgResponse.BodyBytes); var result = ocr.Read(input); // 3. Output the extracted string value Console.WriteLine($"OCR Result: {result.Text}"); } ``` ### OCR 範例輸出 。 ### 範例 3:使用 IronBarcode 管理庫存 [鐵條碼](/static-assets/ironsoftware/suite/blog/curl-dotnet/curl-dotnet-6.webp) [**IronBarcode**](https://ironsoftware.com/csharp/barcode)是一個多功能的函式庫,設計用於[讀取](https://ironsoftware.com/csharp/barcode/tutorials/reading-barcodes)和[寫入](https://ironsoftware.com/csharp/barcode/tutorials/csharp-barcode-image-generator)幾乎任何條碼格式,從標準的 UPC 和 EAN 到複雜的 QR 碼和 Data Matrix 標籤。 它是為了容錯而建立的; 該函式庫包含自動 [ 影像矯正](https://ironsoftware.com/csharp/barcode/how-to/image-correction)濾鏡,可以銳化、潛在二值化和旋轉影像,即使影像損壞、歪斜或光線不足,也能偵測到 BarCode。 這使得它成為物流、零售和工業應用中沒有硬體掃描器的必備工具。 在這個情境中,我們使用 CurlDotNet 精確的網路控制,從安全的 API 取得標籤,並使用 IronBarcode 強大的 [ 讀取引擎](https://ironsoftware.com/csharp/barcode/how-to/read-barcodes-from-images),即時確認內容。 ```cs using CurlDotNet; using IronBarCode; class Program { private static readonly HttpClient client = new HttpClient(); public static async Task Main(string[] args) { // 1. Define the URL string url = "https://barcodeapi.org/api/128/Shipping-Label-Test-123"; try { // Add the session cookie to the request headers client.DefaultRequestHeaders.Add("Cookie", "session_id=xyz123"); // 2. Download the image data as a Byte Array (Preserves binary integrity) byte[] imageBytes = await client.GetByteArrayAsync(url); Console.WriteLine($"Downloaded {imageBytes.Length} bytes."); // 3. Read the barcode directly from the byte array var result = BarcodeReader.Read(imageBytes); foreach (var barcode in result) { Console.WriteLine($"Detected Format: {barcode.BarcodeType}"); Console.WriteLine($"Value: {barcode.Value}"); } } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } } ``` #### IronBarcode 控制台輸出 。 ## 將"使用者領域"帶入 .NET CurlDotNet 將"Userland"的概念帶入 .NET 不只是提出請求。 它可以讓您在執行 Linux macOS 或 windows 的 CI 管道或 docker 容器等地方使用標準的 curl 功能。 您可以使用它在 DotNet run 上下文中使用標準 bash 語法下載檔案、上傳資料或觸發 webhook。 這將縮短命令列工具世界與您的編譯應用程式之間的距離。 ## 結論:翻譯稅的終結 這不僅僅是提出 HTTP 請求; 尊重開發人員的時間。Jacob Mellor 和 Iron Software 明白,如果像 curl 這樣的工具已經完美運作了 25 年,.NET runtime 應該擁抱它,而不是強迫您重新實作它。 採用 CurlDotNet,您不只是增加了一個依賴; 您採用的工作流程是優先出貨功能,而非撰寫模板。 您停止"翻譯"並開始執行。 無論您是抓取單一的 JSON 檔案,或是協調複雜的 Iron Software 文件工作流程,指令都是一樣的:貼上、執行、完成。 ## 後續步驟 不要再浪費時間翻譯標頭和調試 HttpClient。 加入 Userland.NET 運動。 1.**檢查 GitHub:**請造訪 [jacob-mellor/curl-dot-net](https://github.com/jacob-mellor/curl-dot-net) 查看原始碼、文件和範例目錄。 2.**下載 NuGet 套件:**執行 DotNet 新增套件 CurlDotNet 以安裝函式庫。 3.**探索 Iron Software:**看看 [IronPDF](https://ironpdf.com) 和 [IronOCR](https://ironsoftware.com/csharp/ocr) 如何與 CurlDotNet 配合使用,加速您的專案。 利用 CurlDotNet,您可以確保您的 curl 指令和 C# 程式碼使用完全相同的語言。